Notes from Thu May 28, 2009: a) in lecture we had a great interactive time and we developed a program together import random user = 0 computer = 0 while True: n1=random.randrange(-50,50) n2=random.randrange(-50,50) answer = int(raw_input("What is " + str(n1) + "+" + str(n2)+ "?")) total = n1 + n2 if answer == total: user += 1 print "Good." else: computer += 1 print "Bad." print "Score now is ", user, " - ", computer This is the program we used as moderator: import random size = int(raw_input("How many: ")) words = ( "korey", "chaitanya", "asaf", "linwood", "daniel", "velocity", "rick", "hiu lam", "henry", "myung hoo", "dgerman" ) for count in range(size): number = random.randrange(len(words)) a = " " + str(count + 1) print a[-3:] + ".", words[number] We then went to the lab and solved more problem. At the end of the day we posted everything on the What's New? page and a link to a study guide for the exam: http://www.cs.indiana.edu/classes/a201-dger/sum2009/weekThreeStudyGuide.txt In lab these were the problems we worked out: balance = float(raw_input("Initial balance: ")) interest = float(raw_input("Interest rate: ")) / 100 / 12 expenses = float(raw_input("Monthly withdrawal: ")) if interest * balance < expenses: months = 0 while balance + balance * interest - expenses >= 0: months = months + 1 balance = balance + balance * interest - expenses print months, balance print months / 12, "years", months % 12, "months" print "Ending balance:", balance else: print "You will never run out of money." We mentioned that in some cases the calculation becomes unstable. When does that happen? We said we'll be looking at that next week, when we have time. Then we developed this program: count = 0 sum = 0 line = raw_input("Enter: ") while line != "bye": sum = sum + float(line) count = count + 1 print sum / count line = raw_input("Enter: ") Then Linwood came and helped us write the last remaining program: import random num = random.randrange(101) guess = int(raw_input("I have a number 0-100. You get 6 guesses: ")) print num count = 1 while guess != num and count < 6: if guess > num: print "Try lower. ", else: print "Try higher. ", guess = int(raw_input("Guess again: ")) count += 1 if guess != num: print "You lose." else: print "You win and it took you ", count, "guesses." That was it for Thu.