Programming for Teens: Beginning Python Tutorial
In this Python tutorial you’ll be introduced to computer programming using one of the most beginner-friendly languages available. By .
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Programming for Teens: Beginning Python Tutorial
30 mins
Game Time!
The guessing game will generate a random number, then use a loop to ask the player repeatedly for their guess. If the user guesses the number, it'll exit the loop; otherwise the program will notify the user if their guess was too high or too low and continue to ask the player for guesses until they get it right.
You'll begin by generating a random number that will be used for the player to guess.
Add the following code to the Python file you just created:
import random
number = random.randint(0, 100)
The above code imports the random
module, then generates a random number between 0 and 100 and stores it in the variable number
.
Next, you need to ask the player for their guess. Add the following code to the bottom of your program:
guess = raw_input("Guess the Number: ")
This code, as you can probably guess, asks for user input and assigns it to the variable guess
. Remember, that variable is now set to a string — you'll need to convert that string to an integer.
Add the following code to your program, right after the guess
statement you added above:
guess_int = int(guess)
This code sets the variable guess_int
to the integer value of the string you received from the user.
Next you need to compare the user's guess to the randomly selected number.
Add the following code to the end of your program:
if number > guess_int:
print "Too low!"
if number < guess_int:
print "Too high!"
if number == guess_int:
print "You got it!"
Run your program by clicking Run\Run Module in the menu bar or entering python guess.py
in Terminal; enter a number when your program prompts you. What happens?
The program stops as soon as you enter a number and it outputs the result to the screen. Whoops!
You want the program to continue looping until the person guesses correctly. You'll add this functionality by using a variable named running
.
Add the following code just after the import
statement:
running = True
running
will be used with a while loop to control the looping of your program. When the user guesses the correct number, the program will set running
to False
and the while loop will stop.
Add the following code right before the statement where you ask the user for a number:
while running:
guess = raw_input("Guess the Number: ")
...etc
Next, indent the remainder of the program by one level so the while loop recognizes this as the code block for the loop.
Finally, you need to add the logic that sets running
to False
once the user wins.
Add the following code directly after the third print
statement:
if number == guess_int:
print "You got it!"
running = False
Make sure that the two lines below the if
statement are indented by two levels.
Run your program, and try it out now. How many guesses will it take you to guess the right number?
Guess the number: 50
Too Low!
Guess the number: 75
Too Low!
Guess the number: 87
Too High!
Guess the number: 80
Too Low!
Guess the number: 82
Too Low!
Guess the number: 84
Too High!
Guess the number: 83
You Got It!
Your completed program should now look like the following:
import random
running = True
number = random.randint(0, 100)
while running:
guess = raw_input("Guess the Number: ")
guess_int = int(guess)
if number > guess_int:
print "Too low!"
if number < guess_int:
print "Too high!"
if number == guess_int:
print "You got it!"
running = False
Congratulations — you've just written your first Python program. That wasn't so hard, was it? :]
Where to Go From Here?
Now that you've successfully completed this Python tutorial for beginners, I bet you're itching for a challenge. Try adding the following features to your program:
- A running count of guesses
- A bigger range of numbers to pick from
- A computer opponent to compete against
If you are looking to make a slightly more complicated game, check out one of my other tutorials: Beginning Game Programming for Teens with Python.
If you have any comments or feedback, feel free to share them in the forum!