1. Write a program that creates random addition questions (by generating random integer operands between -50 and 50) and asks the user to solve them. Your program should keep track of the number of good answers as well as of total questions asked. Variations: program asks ten questions then ends; another one that ends only when the user types "done". Either way the program should report a score at the end. 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 ------------------------------------------------------------------------------------------------- 2. Write a program that randomly picks an integer between 0 and 100 and then asks the user to guess it. The game ends when the user guesses the number. Each time the user enters a guess feedback is provided (try higher, try lower) and a count is kept that records the number of incorrect guesses made up to that point. Report this number when the game ends. When you're done, your task is to modify this program so that if the user makes 6 mistakes the user loses the game. 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." ------------------------------------------------------------------------------------------------- 3. Write a program that expects the user to enter a number per line and prints back (after every line) the current average of all the numbers entered up to that point. Program stops when the user enters the string "bye". 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: ") ------------------------------------------------------------------------------------------------- 4. First, write an algorithm to settle the following question: A bank account starts out with $10,000. Interest is compounded at the end of every month at 6 percent per year (0.5 percent per month). At the beginning of every month, $500 is withdrawn to meet college expenses after the interest has been credited. After how many years is the account depleted? Now suppose the numbers ($10,000, 6 percent, $500) were user-selectable. Are there values for which the algorithm you developed would not terminate? Here's the program that reads the three values and produces the correct answer: 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." ------------------------------------------------------------------------------------------------- 5. Strings have a split method that works like this: >>> a = "this is a sentence" >>> a.split() ['this', 'is', 'a', 'sentence'] >>> Knowing this write a program that acts as a vending machine as illustrated below: a) you can enter names of coins, one per line or any number per line b) the program updates the balance as it recognizes the coins c) you can stop the program with "bye" (alone on a line) Welcome to the Vending Machine! coins> dime Balance becomes: $0.10 coins> dollar dime dime Balance becomes: $1.10 Balance becomes: $1.20 Balance becomes: $1.30 coins> quarter cent Balance becomes: $1.55 Balance becomes: $1.56 coins> cent cent cent Balance becomes: $1.57 Balance becomes: $1.58 Balance becomes: $1.59 coins> dime nickel dime dime nickel Balance becomes: $1.69 Balance becomes: $1.74 Balance becomes: $1.84 Balance becomes: $1.94 Balance becomes: $1.99 coins> bye Thanks for using this program. The program should report bad coins: Welcome to the Vending Machine! coins> dime dime mark dinar nickel ruble Balance becomes: $0.10 Balance becomes: $0.20 I don't understand mark Balance becomes: $0.20 I don't understand dinar Balance becomes: $0.20 Balance becomes: $0.25 I don't understand ruble Balance becomes: $0.25 coins> quarter Balance becomes: $0.50 coins> shekel I don't understand shekel Balance becomes: $0.50 coins> bye Thanks for using this program. The program should ignore empty lines: Welcome to the Vending Machine! coins> coins> coins> coins> coins> dime Balance becomes: $0.10 coins> coins> coins> dime Balance becomes: $0.20 coins> nothing I don't understand nothing Balance becomes: $0.20 coins> bye Thanks for using this program. Here's the solution we worked out in class: print "Welcome to the Vending Machine!" line = raw_input("enter> ") balance = 0 while line != "done": coins = line.split() for coin in coins: if coin == "dime": balance += 10 elif coin == "quarter": balance += 25 elif coin == "nickel": balance += 5 else: print "I don't understand", coin print "$" + str(balance / 100.0) line = raw_input("enter> ") print "Thanks for using this program." ------------------------------------------------------------------------------------------------- 6. Write a program that plays hangman with the user as described below. >>> three ----- Guess a letter: h -h--- Guess a letter: x Oops, this was your 1 th mistake! -h--- Guess a letter: x I told you before, x is not in the secret word. -h--- Guess a letter: e -h-ee Guess a letter: a Oops, this was your 2 th mistake! -h-ee Guess a letter: e -h-ee Guess a letter: i Oops, this was your 3 th mistake! -h-ee Guess a letter: r -hree Guess a letter: u Oops, this was your 4 th mistake! -hree Guess a letter: t three Very good, you guessed the secret word! >>> Here.s another session in which I lose: >>> two --- Guess a letter: a Oops, this was your 1 th mistake! --- Guess a letter: e Oops, this was your 2 th mistake! --- Guess a letter: i Oops, this was your 3 th mistake! --- Guess a letter: o --o Guess a letter: u Oops, this was your 4 th mistake! --o Guess a letter: a I told you before, a is not in the secret word. --o Guess a letter: o --o Guess a letter: m Oops, this was your 5 th mistake! --o Guess a letter: n Oops, this was your 6 th mistake! Sorry, you lost the game, the secret word was: two >>> Here's the solution: import random words = ("python", "exercise", "tangerine", "cloud", "water") secret = words[random.randrange(len(words))] print secret print "Welcome to the game!" dashes = "-" * len(secret) while dashes != secret: guess = raw_input(dashes + ": ") if guess in secret: for index in range(len(secret)): if secret[index] == guess: # we'd like to write dashes[index] = guess but we can't, so we write dashes = dashes[0:index] + guess + dashes[index+1:] # this, to the same effect else: print "Nope." print "You guessed the word, congratulations!" -------------------------------------------------------------------------------------------------