Introduction: How to Make a Python Guessing Game

Welcome to the world of python. Here we are going to make a basic guessing game. Enjoy!

Step 1: Download Python 2.7, NOT 3.x

First you have to download python 2.7 from python's official site. https://www.python.org/downloads/release/python-27...

Don't download it from other sites, they sometimes might insert some form of malware in it.

Download your specific version your operating system.

Also, download the latest version of python 2.7.x that there is. It will make no difference at all.

Go through the installation process. Congratulations, you completed step 1!!!

Step 2: Starting Your Guessing Game

Now that you have Python 2.7.x installed, you can start making the guessing game. Open a python shell.

You should have something that looks similar to what I have shown above.

Now we can start making the game.

Step 3: The Start

So, you have your shell open now. Great, let's start now.

Go to file, new file, and click that. You should now have a blank space where you can start typing.

Then type exactly what I have.

code:

import random

import time

So what we are doing is asking python to bring some things in. You can probably guess that importing random will help us design the part where a random number is chosen. The import time is just once you finish the game, it will close after a certain amount of time. Good job so far!

Step 4: Using Variables

So far you should have

import random

import time

This is good. Now we are going to set the random part.

Type this exactly

code:

num = random.randint(1, 101)

What this is doing is setting a variable that is a random integer between, 1-100. "But Jason, it says 1, 101." Well yes it does, but python does it up to 101, not over. So this means you have to go one over the range you want, if that makes any sense. This is same with loops, (if you want to learn python, you will learn about this later)

Step 5: Making Important Things

So now, you should have this so far

import random
import time

num = random.randint(1, 101)

Now we are going to add more code.

Here is what we are going to add:

code:

while True:
print 'Guess a number between 1 and 100'

guess = input()

i = int(guess)


Now you have to have the stuff below while True indented. You also need a colon too. What this piece of code is saying is print Guess a number between 1 and 100. That is what the user will see when we run this program.

next we set a variable called guess to take input from a user

Then we set another variable that is called i that turns the user's input to a integer. You see, when you type in something, it is always a string. So if you didn't convert it, python would give you an error. Now that it is an integer, we can start using it. Great job so far!!!

Step 6: Final Steps

So you've made it this far! That's great.

This is what you should have so far:

Code:

import random
import time

num = random.randint(1, 101)

while True:

print 'Guess a number between 1 and 100'

guess = input()

i = int(guess)

if i == num:

print 'You guessed right!'

print 'This window will close in 5 seconds now.'

time.sleep(5)

break

# that pound symbol is a comment by the way, don't put this in the code. I can't indent here for some reason so just look at the picture I provided to see how I did it. I do apologize.

elif i < num:

print 'Try Higher'

elif i > num:

print 'Try lower'

What this does is if you guessed the exact number, it will say you guessed right! It will close in 5 seconds.

elif is if you didn't guess right and then if the number was smaller that what you said, it said go lower, etc.

Now lets go to the final step...

Step 7: Ending It

So here is the final code

Look at the picture in the previous step, it shows it much better!

So now you just finished your guessing game. So here it is. Lets try it. Type in power-shell if you are in windows. You also should save this to your desktop. Then I will show you how to do it!

So first I change directories with (cd) then desktop. Then type python whateveryounamedit.py

Then go ahead and try it.

Have fun with your guessing game. Thanks for viewing my tutorial. I hope you enjoyed it!