For this assignment, you will need to make a robust version of the "guess the number" game. It should be robust to bad input, and it should select a random number between 1 and 100 (inclusive) as the answer. If the user tires of the game, then they should be able to quit early by typing the word 'quit' instead of a number.
You should use the random module to
generate a random number between 1 and 100 (inclusive), and the code we discussed today and the .isdigit()
method should be helpful for the error checking.
Also, I recommend building the game piecewise. I've been trying to do this in class, but I've been quiet about it. Get a game with a subset of the features working, then add features one by one and make sure each feature works in turn. This helps ensure that you are working on a stable and correct foundation. When you try to build something all at once, it's hard to know where the problem is when it doesn't work. When it used to work, and you only changed a few things, then discovering the source of any problems is much easier.
Here is a sample interaction with the program:
Guess a number between 1 and 100 Type 'quit' to exit early 50 Too high -sdfg That is not a number or the word 'quit'. Please try again. asdf That is not a number or the word 'quit'. Please try again. 324-fasd That is not a number or the word 'quit'. Please try again. 123-23 That is not a number or the word 'quit'. Please try again. 1e2 That is not a number or the word 'quit'. Please try again. 1e+2 That is not a number or the word 'quit'. Please try again. 1.2 That is not a number or the word 'quit'. Please try again. 25 Too high 12 Too high 6 You win! Thanks for playing
Here's another sample interation:
Guess a number between 1 and 100 Type 'quit' to exit early quit Goodbye Thanks for playing
For extra credit, you may add features to the game to make it more interesting and fun - for example, 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, or you might add the ability to do negative numbers, or the ability to play multiple games without having to restart the program.
Good luck, and don't hesitate to contact me with questions. If you post comments and questions below, then everyone else can read the question and answer and hopefully we will all become less confused over time.
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!