A5 - Guessing Game, due Sun, Nov. 11

Assigned Monday, Oct. 29; Due Sun, Nov. 11 at 11:59 PM;

Before starting this assignment, you should review the Python lecture notes, including the exercises at the bottom.

In this assignment, you will write and test a short numeric guessing game in Python. Complete this assignment in our online Python classroom (repl.it/classroom/) and press ‘submit’ when you’re finished.

Requirements

Here are the game requirements:

  • Your program MUST have a comment at the top containing your name, the date, and a description of the game.
  • The game must start by asking for the player’s name. It can then use that name throughout in creative ways, such as “Lucky guess, Ralph!” or “Thanks for playing, Samantha!”
  • Next, the program chooses a random number between 1 and 100. To do this in Python, you need these lines:
    import random
    secretNumber = random.randrange(1,101)
    

    and then the variable secretNumber will contain the random number.

  • The player repeatedly guesses the number, and the program provides feedback such as “too high” or “too low.”
  • The program should count how many guesses the player makes, and report that at the end.
  • Once the player guesses the correct number, the program should announce that the game is over, and it should end.
  • Complete one or more of the following extras

Extras

  • Give the player a limited number of guesses, such as 6 or 7. If he or she doesn’t get the correct number in that many guesses, the game is lost.
  • Give the player a difficulty option. The easy game is a number between 1 and 100, but the difficult game goes 1 to 1,000. (If you are also doing the limited number of guesses, then the larger range can allow up to 9 guesses.)
  • Find a way to respond politely if the user enters an invalid response. For example, if the user enters “ugh” then the program will probably crash with an error. Instead, make it say “Sorry, want to try that guess again?”

Hint(s)

If you get an error that looks something like this:

Traceback (most recent call last):
  File "main.py", line 13, in <module>
    if guess > secretNumber:
TypeError: '>' not supported between instances of 'str' and 'int'

Can you guess why this error happens? Check out the lecture notes on strings for more information.

Transcripts of sample games

Here are some transcripts of sample games that I played with my solution. The user’s input is indicated by «angle quotes».

Game 1

Alice paid attention in algorithms class and is using a binary search to play the game.

Welcome to the guessing game.
What is your name? «Alice»
I'm thinking of a number between 1 and 100.
Your guess? «50»
Too high!
Your guess? «25»
Too low!
Your guess? «38»
Too high!
Your guess? «31»
Too low!
Your guess? «34»
Too high!
Your guess? «33»
Yes, my secret number was 33
Congratulations, Alice you got it in 6 guesses

Game 2

Bob doesn’t know about binary search, so it takes him much longer.

Welcome to the guessing game.
What is your name? «Bob»
I'm thinking of a number between 1 and 100.
Your guess? «10»
Too low!
Your guess? «20»
Too low!
Your guess? «30»
Too low!
Your guess? «40»
Too low!
Your guess? «50»
Too low!
Your guess? «60»
Too low!
Your guess? «70»
Too high!
Your guess? «65»
Too low!
Your guess? «66»
Too low!
Your guess? «67»
Too low!
Your guess? «68»
Too low!
Your guess? «69»
Yes, my secret number was 69
Congratulations, Bob you got it in 12 guesses

Game 3

Carla is a little haphazard with the binary search, but still does pretty well.

Welcome to the guessing game.
What is your name? «Carla»
I'm thinking of a number between 1 and 100.
Your guess? «50»
Too low!
Your guess? «75»
Too low!
Your guess? «90»
Too high!
Your guess? «82»
Too high!
Your guess? «78»
Too low!
Your guess? «80»
Too high!
Your guess? «79»
Yes, my secret number was 79
Congratulations, Carla you got it in 7 guesses

© 2020. Some rights reserved.