0% found this document useful (0 votes)
4 views

Make A Number Guesser in Python!

Uploaded by

bloodscar96
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Make A Number Guesser in Python!

Uploaded by

bloodscar96
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Use https://www.programiz.

com/python-programming/online-compiler/ to write and run your


code.

1. Setting up your code.


We need to include 2 modules. Modules are add-ons of special functions that can help your
code. We are going to add the “random” module, which lets us generate random numbers. We
are also going to add the “string” module, which lets us work with letter and number input a
person types.
Add these first lines to your project:
import random
import string

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:

guess = input("Enter any number: ")

You can put your own message in the quotation marks, but make sure you leave a space after
whatever you end up writing.

2. Next, we need to generate a random number. We can use the random.randrange()


function for this. Set up another variable called n that is equal to your random number.
You can make your “range” whatever size you want to make your game easier or more
difficult.

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:

The != is python for “not equal to.”

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 <.

if #add your inequality here:


print("Too low")

elif #add your inequality here:


print("Too high!")
guess = int(input("Enter number again: "))
else:
break
`
“Elif” means else+if.

7. If our guesses are wrong, we need to ask for another guess.


Reuse your code that takes in a guess and check if it’s a number. Copy and paste it
inside your while loop as many times as you need.

8. After your while statement, add a print()to tell your player that they won.

You might also like