COS 100: Introduction to Programming
Interim 2022
HW Project 05: Guessing game
Due: 01/14 Fri 10pm
O Lord, You have searched me and known me.
You know my sitting down and my rising up;
You understand my thought afar off.
You know my sitting down and my rising up;
You understand my thought afar off.
Psalm 139:1–2
Project goal
Create a numbers guessing game.Project specification
- Ask the player if they would like to play a game. If they say no, say "Farewell" and quit the program.
- If they want to play, generate a random integer between 1 and 100.
-
Now give the player up to six rounds to guess the secret number.
- If they enter a number that is not between 1 and 100, tell them what they did wrong, and move on to the next round.
-
If they guess the secret number, tell them they got it right, and ask if they want to play again.
- If the user wants to play again, go back to step 2 above.
- If the user doesn't want to play again, say "Farewell" and quit the program.
- If they guess the wrong number, tell them whether they are too high or too low, and move on to the next round.
-
If the user runs out of rounds without guessing correctly, tell them what the secret number was, tell them they lose, and ask if they would like to play again.
- If the user wants to play again, go back to step 2 above.
- If the user doesn't want to play again, say "Farewell" and quit the program.
Notes and hints
-
Name your project source code
guess.py
. -
You may assume that the user always enters either
yes
orno
when asked a yes/no question. - You should remember how random number generation works from the previous project.
- This assignment is meant to demonstrate your knowledge of looping, as well as continuing to use decision structures. If you simply copy and paste the same code multiple times for the six rounds, you will lose points. Collecting and processing each guess will be fundamentally the same as the other guesses, so use looping to collect guesses and provide feedback to the user as needed.
- Create a flowchart for how your program should run before you start coding. A correct submission will not necessarily be very long in terms of lines of code if you think carefully about how to structure it. Concentrate on having a good plan, then focus on implementing it.
- During development, you can (and should) print out the secret number at the beginning of each game to help you test your program; just don't forget to take that out when you're done.
Suggested order of development
As always, try to code this up a little bit at a time, testing after each feature you've added.- Repeatedly ask the user if they want to play a game, generate (and print!) a new secret number each time, and keep asking again and again until they indicate they are tired of this game.
- Let them make one guess and provide appropriate feedback on whether their guess was too high, too low, or correct.
- Add the looping to give them up to 6 guesses.
- Take out the code that prints out the secret number at the beginning of each game.
Sample runs
- The secret numbers were 61 and 3.
Greetings. Would you like to play an awesome game? (yes/no) yes You have 6 chances to guess my secret number. Guess a number between 1 and 100: 50 Sorry, 50 is too low. You have 5 chances left. Guess a number between 1 and 100: 75 Sorry, 75 is too high. You have 4 chances left. Guess a number between 1 and 100: 65 Sorry, 65 is too high. You have 3 chances left. Guess a number between 1 and 100: 58 Sorry, 58 is too low. You have 2 chances left. Guess a number between 1 and 100: 63 Sorry, 63 is too high. You have 1 chances left. Guess a number between 1 and 100: 60 Sorry, 60 is too low. Oy vey, out of chances. The secret number was 61. You lose! Would you like to play again? (yes/no) yes You have 6 chances to guess my secret number. Guess a number between 1 and 100: 50 Sorry, 50 is too high. You have 5 chances left. Guess a number between 1 and 100: 25 Sorry, 25 is too high. You have 4 chances left. Guess a number between 1 and 100: 12 Sorry, 12 is too high. You have 3 chances left. Guess a number between 1 and 100: 6 Sorry, 6 is too high. You have 2 chances left. Guess a number between 1 and 100: 3 Congratulations! The secret number was 3 Would you like to play again? (yes/no) no Farewell.
- Handling inappropriate inputs.
Greetings. Would you like to play an awesome game? (yes/no) yes You have 6 chances to guess my secret number. Guess a number between 1 and 100: 123 You were supposed to enter a number between 1 and 100. You have 5 chances left. Guess a number between 1 and 100: -50 You were supposed to enter a number between 1 and 100. You have 4 chances left. Guess a number between 1 and 100: 0 You were supposed to enter a number between 1 and 100. You have 3 chances left. Guess a number between 1 and 100: 100 Sorry, 100 is too high. You have 2 chances left. Guess a number between 1 and 100: 1 Sorry, 1 is too low. You have 1 chances left. Guess a number between 1 and 100: 50 Sorry, 50 is too low. Oy vey, out of chances. The secret number was 93. You lose! Would you like to play again? (yes/no) no Farewell.
Optional challenge
Reverse the role of the computer and the user.That is, ask the user to commit to a number (perhaps by writing it down on a sheet of paper). The computer will now start to guess numbers. The user must reply whether the guess is right, too high, or too low. The computer has 6 chances to guess it.
If you want, you may submit this to Moodle as sseug.py
,
but it will not be graded.
Start early, have fun, and discuss questions on Moodle.