#------------------------------------------------------------------------------- # Function3.py # functions have return values #------------------------------------------------------------------------------- def sum_to(n): """returns sum of numbers from 1 to n """ # this is a doc string s = 0 for x in range(1, n+1): s += x # same as: s = s+x return s # end of function sum_to() def fact(n): """returns product of numbers from 1 to n """ # another doc string p = 1 for x in range(1, n+1): p *= x # same as: p = p*x return p # try commenting out the return statement # end of function fact() #-- main program --------------------------------------------------------------- for n in range(1, 41): print(n, '\t', sum_to(n), '\t', fact(n)) # print help on functions sum_to and fact, uses doc strings help( sum_to ) help( fact ) # another way to print out doc strings print( sum_to.__doc__ ) print( fact.__doc__ )