class logo

CS 122 - Day 13 Exercises

This page lists some problems for today's class. After today, you should be able to solve these problems on your own. Problems with this color are more challenging.

Guessing Game

On day 7 of the class we implemented a simple guessing game. The program randomly selected a secret number, then asked the user to guess the number. After each guess, the program tells the user whether their guess is too low or too high. here's my solution for that exercise:

import random secretNumber = random.randint(1, 100) shouldContinue = True while shouldContinue == True: userInput = raw_input("Guess a number (q to quit): ") if userInput == 'q': shouldContinue = False else: guess = int(userInput) if guess == secretNumber: print "Correct!" shouldContinue = False elif guess > secretNumber: print "Too high!" else: print "Too low!"

In today's exercise we'll extend this program a bit. We'll add error checking to make sure that the user enters a valid number for each guess, and add a timing feature to report how long it takes the user to guess the number.

1) Error Handling

In the original program, if the user enters a guess that is not 'q' and not a number, the program dies with an error message. Try it out for yourself to see what happens.

We want to add input validation so that the user doesn't see an error if they enter an invalid number. Use a try/except block to ensure that if the guess isn't an int we can display an appropriate message to the user.

My solution

1.A) Input Validation

An alternative to using the try/except block is to examine the users input before trying to convert it to an integer. There is a string function isdigit() that checks to see if a string is made up of nothing but numbers. If that is the case, we should always be able to convert it to an int. Alter your program from the above section to check the users input using isdigit() instead of using the try/except block. Is one version simpler or easier to follow than the other?

My solution

2) Timing

Let's add an additional feature to our game. Let's time the user to see how long it takes them to guess the secret number. Use the time library to record the time when the user starts the game and then again to record the time when the game ends. The end time minus the start time gives you the elapsed time. Report this to the user when they correctly guess the secret number.

My solution

3) High score

Let's record the high score from our secret number game into a file. When the game starts, it should load the file and display the fastest time with the users initials. When the game is over, check to see if the current score is better than the one in the file. If it is, ask the user for their initials and overwrite the high score file with the new score.

Note: the first time you run the game, there won't be a high score file so your code to open the file and display the high score will fail. This is a good place to use a try/except block to catch the file open error.

My solution

3.A) More high scores

Rather than just storing a single high score, store the top 10 high scores. This gets a bit trickier, because you need to figure out where the current high score fits in the overall list.