# Midterm Exam #1 # Review Sheet Hints, Answers and Code # 1. Let num be the number entered. Use the lines while num < 10 or num > 99: num = input("That wasn't a two-digit number. Try again: ") # to get the while loop working as desired. # 2. The really tricky part of this program is to figure out # how to test whether the three numbers are NOT all the same. # Here's one way to accomplish the task. import random die1 = random.randint(1,6) die2 = random.randint(1,6) die3 = random.randint(1,6) print "\t", die1, "\t", die2, "\t", die3 while die1 != die2 or die2 != die3: # The rest of the program continues from here. # 3. You should find that there are 257 numbers in the list. # 4. Assuming that "cents" is your variable, type quarters = cents/25 cents = cents - 25*quarters # to figure out the number of quarters. # Now do the same for dimes, etc. # 5. Let "digit" be the random number generated. Then type for j in range(1,digit+1): print "\t", print digit # to tab over the correct number of times. # 6. There are three such numbers: 351, 702, 711 # 7. You will need to keep track of how many guesses the user has made. # Assuming you use the variable `tries', your while loop condition # could look something like this while tries < 7 and guess != "hot pink": [give user another guess here, and add 1 to tries] # 8. On your review sheet, draw a diagonal line from the upper right # to lower left corner. Now figure out how to draw each half of # the diagram separately. In each case try a for loop that looks like for j in range(0,201,20): # 9. Note that the centers of the circles are spaced every # 20 pixels. So set up a nested for loop that looks like for x in range(20,181,20): for y in range(20,181,20): if [use correct condition here]: pygame.draw.circle(screen, [0,0,0], [x,y], 5, 1) # What is true about x+y for the circles we want to draw? # 10. Suppose the user enters values of x and y for the corner. # After drawing the rectangle, use a for loop such as for j in range(x+10,x+80,10): pygame.draw.line(screen, [0,0,0], [j,y], [j,y+30]) # to get the vertical lines inside the rectangle. # 11. This is an exercise in using and/or correctly. You will find # that parentheses can come in handy here. For instance, if the # guesses are called `name' and `age', then the first statement is if (name == "Obama" or name == "obama") and age == 51: print "You are a wallking encyclopedia of useless trivia!" # 12. There is one potential pitfall here; namely, trying to use variables # inside an input statement. To get around this problem, try this: # (Notice the comma after the print statement.) print "And how many", item, "do you need?", quantity = input("")