Challenge: Let’s Make a Game
So now you’ve learned about:
print
statements for talking to your users- variables, which are a way to get our program to remember and update values
- strings, which are pieces of text
input
for getting information from our user- maths: how to do maths with a number
- integers, which are numbers for doing maths with
if
statements, to make your code do something based on a conditionwhile
loops, to make your code keep doing something until a condition isn’t true
You can use these tools and commands to make this game:
- There is a number (an integer), between 1 and 9, that the program picks in secret
- Players have 5 guesses to pick the number
- The game teaches players the rules
- Players are told after each guess whether the number is lower, higher, or correct, and how many guesses they have left
- If players gets their guess right, they get a special winning message
- If players gets their fifth guess wrong, the game is over and they lose
You can play an example of the game at dojo.soy/py-dice.
You’re missing just one thing to be able to write this game: a way to get a random number between 1 and 9. The code to do this is a little beyond what you’ve covered, but you can use it right now without understanding exactly how it works.
Put this as the first line in your program:
from random import randint as dice
Now, anywhere you want to use a random number between 1 and 9, just use dice(1,9)
. For example:
secret_number = dice(1,9)
Start a new Python program and try to make the game now!
Remember to use what you’ve learned on previous steps of this project.
Good luck!
I need a hint
If you’re stuck, or when you’re done, you can check my answer at dojo.soy/python-ans. Don’t worry if yours looks very different — that doesn’t matter as long as it works.