In this project, you’ll write a guess-my-number game. When you’re done, it’ll look something like this:
You’re basically on your own for this one! There’s no starter code - write this program from scratch! You can do it!
You’ll be using a while
loop - possibly a while True:
loop with a break
statement somewhere in there, possibly some other variant, it’s up to you. Go back to the slides to remind yourself how while
loops work.
You’ll definitely be using at least one if
statement.
Your program should choose a different number each time it’s run - don’t just hardcode the number 7 in there! We’ll be checking to make sure that your program picks a different number each time.
You’ll want to use the random.randrange()
function in order to choose a number. Here’s how I might use it in order to pick a number between 5 and 10:
import random
# `random.randrange()` picks a random number
# in the specified range each time it's called:
print(random.randrange(5, 11))
print(random.randrange(5, 11))
print(random.randrange(5, 11))
print(random.randrange(5, 11))
# To rerun this code snippet: click on this text box,
# hold the Control key, and then press Enter a bunch of times.
One last tip: While you’re working on this project, it’s probably a good idea to have the program print out the secret number at the start of the game. This way, when you’re working on the program, you can see what the number is and use that information to figure out how best to test the program.
For instance, if I know the number’s 37
, I might test the program by giving it 40
and then 35
and then 37
, and seeing if my program does the right thing each time.
If you do that, though, be sure to remove that line before submitting your finished program. The game’s no fun if the player knows the number without having to guess!
Once you’re done with the basics, add more cool features to your program! Here are some suggestions:
Nice try JR, but your number was way too low!
, that sort of thing.1
and 100
, ask the user for the maximum number to use, and then have the program use that number as the largest possible secret number to guess. Heck, you could even let them specify the minimum number of the range too, so they can guess a number between -50
and -8
if they want!These are just some ideas - you can do some or all of these, or come up with completely different ideas of your own! Go nuts!
Submit a file called guess_my_number_<YOUR_NAME>.py
. For instance, I’d submit a file called guess_my_number_jr_heard.py
.
On the first line of that file, write a comment with your name on it, like this:
# JR Heard
Remember to follow this class’s style guide.
The part about descriptive variable names is really important! For instance:
n
is a bad variable name, username
is a good one.ns
is a bad variable name, number_of_symbols
is a good one.