For this assignment, you will need to combine the two things we discussed in class today - error checking and our silly game.
So, to be very specific about this, I would like you to write a number guessing games that uses the random module to generate a random number between 1 and 100 (inclusive) and then tries to get the user to guess that number by providing the feedback "Too high" or "Too low". If the user guesses the exact answer, it should print out "Just right".
Furthermore, you should protect your code so that a user entering random garbage won't cause your program to crash. I demonstrated both of these things using actual working code in front of the class, so this shouldn't be too hard an assignment.
Here is a sample interaction with the program:
What number I am thinking of? 50 Too high What number I am thinking of? sdafasdf What number I am thinking of? 4343 fsad asdf3 What number I am thinking of? 25 Too high What number I am thinking of? 12 Too high What number I am thinking of? 6 Too low What number I am thinking of? 8 Too low What number I am thinking of? 10 Just right
Either using the try: ... except: idea we discussed in class, or using the .isdigit() method that all strings have should be of great help. For extra credit, you may add features to the game to make it more interesting and fun - for example, you might add the ability to type "q" to quit, or you might have the computer randomly add or subtract one from the target number, so that it is always a bit of a moving target.
Good luck, and don't hesitate to contact me with questions. If you post comments below, I will be able to respond to you and then everyone else can read the answer and hopefully become less confused over time.
0 | |
1 | |
2 | |
3 | 0 |
4 | 0 |
5 | |
6 | |
7 | 0 |
8 | 0 0 5 5 |
9 | 0 5 5 5 7 9 9 9 |
10 | 0 1 2 5 |
Ask me if there are any questions, and remember that elegance counts! peter@cs.uoregon.edu, or simply commenting below will all reach me immediately. Also recommended is coming in to office hours if you have any questions.
Turn your project in using the following form:
Questions? Answers!