COS 100: Introduction to Programming

Fall 2019

HW Project 05: Guessing game

Due: 10/23 Wed 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.
Psalm 139:1–2

Project goal

Create a numbers guessing game.

Project specification

  1. Ask the player if they would like to play a game. If they say no, say "Farewell" and quit the program.
  2. If they want to play, generate a random integer between 1 and 100.
  3. 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.
  4. 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

Suggested order of development

As always, try to code this up a little bit at a time, testing after each feature you've added.
  1. 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.
  2. Let them make one guess and provide appropriate feedback on whether their guess was too high, too low, or correct.
  3. Add the looping to give them up to 6 guesses.
  4. Take out the code that prints out the secret number at the beginning of each game.
At each step, make sure you have a working program before you add more complexity.

Sample runs

  1. 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.
    
  2. 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.