Number guessing game.
- The user needs to guess the number that Python is “thinking” of.
- If the user guesses too low, print “guess is too low”
- If the user guesses too high, print “guess is too high”
This is difficult, and it will take time to get right.
- Don’t worry about mistakes.
- Read your errors. These will help you figure out where your mistakes are!
- Comment on your code! You need at least 3 comments (use your own words)
Break Down the Problem
What do you need Python to do?
- print instructions at the beginning.
- input for the human to guess a number
- while loop for the process to repeat until human guesses correctly
- random number (explained on next slide)
- if – then – elif – else for if the guess is too high print “too high”
- or if the guess is too low print “too low”
Random Numbers – Your first 3 lines of code
We can set the computer’s number to be some random number between 0 and 1000 with the following code.
from random import randint compy = randint(0,1000) human = " "
We want to start the human with nothing, so we make it an empty string.