Assignment 5 – Python

due at 23:59 on   +60

Before starting this assignment you should review the Python notes, including the two exercises at the bottom of that page.

This assignment is an individual activity. It asks you to write and test a short Python program to implement a numeric guessing game. When you are finished, you should upload your .py file to the dropbox at https://www.dropbox.com/request/psrvRwRnwS1SuywrbmaH

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.

Extras

If you have all that working, here are some optional extras you might want to try:

  • 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?”

Transcripts of sample games

Here are some transcripts of sample games that I played with my solution. As in the notes, 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