Make A Number Guesser in Python!
Make A Number Guesser in Python!
Next, we want to get a user’s guess. Set up a variable called guess that takes an input from
the player by writing this:
You can put your own message in the quotation marks, but make sure you leave a space after
whatever you end up writing.
n = random.randrange(1,10)
3. We need to make sure our guess is a number before we start checking if it’s right!
You can check your guess by using a while loop, and a function called
isnumeric(). If the guess is not a number, we keep making the user enter a new input
until the number is valid.
while(not guess.isnumeric()):
guess = input("That's not a number. Enter any number: ")
Try now entering input that is not all numbers into your code and seeing what happens!
4. Now that we know the user inputted a number, we need to make sure that the computer
also is aware the user inputted a number. We do this by changing the type of our guess
variable to “int”, which tells the computer it is an integer.
guess = int(guess)
This might look silly, but now we can treat our guess like it is a number and do math to it!
5. We want to compare our guess to n UNTIL it has been correctly guessed by the player.
We can do this by using a while loop.
while n != guess:
6. We want to give our player a little assistance when they’re guessing and tell them if their
number is “too high” or “too low.” Inside our while loop, copy and paste this code. Add
inequalities comparing n and guess. You can use > or <.
8. After your while statement, add a print()to tell your player that they won.