import string import math #-------------------------------Problem 0------------------------------------- # In the program below: # Circle the identifiers. # Box the reserved words. # Underline the numeric literals. # Underline the string literals. # Diamond the arithmetic operators def fib(): print "compute the Fibonacci Numbers, given some n > 2" fib1, fib2 = 1, 1 thisfib = fib2 lastfib = fib1 n = input ("enter some n > 2: ") for i in range(2,n): newfib = thisfib + lastfib lastfib = thisfib thisfib = newfib print "The fibonacci # %d is: %d" % (n, thisfib) print "Problem 0." fib() # For each of the remaining problems write in the space provided: # a) What each of the following programs prints as output, and # b) A brief description of what the program computes for problems 5 to 11. # # IMPORTANT: Write what it computes, not how it computes. #-------------------------------Problem 1------------------------------------- # Output # ------ def calc(): x = 2 x = x+1 print x*x - 1 print "\nProblem 1." calc() #-------------------------------Problem 2------------------------------------- # Output # ------ def types(): print 4/5, 4.0/5.0, 4/5.0 print "\nProblem 2." types() #------------------------------Problem 3-------------------------------------- # Output # ------ def ranges(): for i in [1,3,5]: print i, print for j in range(3): print j, print for k in range(0,50,10): print k, print for m in range(10,101,20): print m, print print "\nProblem 3." ranges() #------------------------------Problem 4 ------------------------------------- # Output # ------ def converts(): print float(3.1) #-------------- print float(4) #-------------- print int(3.500) #-------------- print round(3.500) #-------------- print int(round(3.500)) #-------------- print round(int(3.500)) #-------------- print "\nProblem 4." converts() #-------------------------------Problem 5------------------------------------- # Output Description # ------ ----------- def loopfun(): n = 3 for i in range(n): print i*i print "\nProblem 5." loopfun() #------------------------------Problem 6-------------------------------------- # Output Description # ------ ----------- def dis(): x1,y1=2,2 x2,y2=5,6 print math.sqrt((x2-x1)**2 + (y2-y1)**2) print "\nProblem 6." dis() #-------------------------------Problem 7------------------------------------- # Output Description # ------ ----------- def accumfun(): n=3 t = 0 for i in range(0,n+1): t = t + i*i print i, t print "\nProblem 7." accumfun() #---------------------------------Problem 8----------------------------------- # Output Description # ------ ------------- def listfun(): mylist = range(0,100,2) acc = [] for n in range(5): acc = acc + [mylist[n]] print n, acc print "\nProblem 8." listfun() #---------------------------------Problem 9----------------------------------- # Output Description # ------ ------------- def sumfun(): n = 4 sum = 0 for i in range(1,n+1): sum = sum + i**3 print i, sum print "\nProblem 9." sumfun() #------------------------------Problem 10------------------------------------- # Output Description # ------ ------------- def moremath(): import math n = 4 acc = 1 for i in range(1,n+1,1): acc = acc * 2 print i, acc print "\nProblem 10." moremath() #------------------------------Problem 11.------------------------------------ # Output Description # ------ ----------- def code(): for s in ["her", "favorite", "show", "is", "my", "favorite", "martian"]: print string.upper(s[1:3]), print print "\nProblem 11." code() #----------------------------Ignore below here ------------------------------- def main(): raw_input("Type enter to quit") print "Done." main()