Coding in Python
Maths Games (Python 2.7)
Start IDLE – this will provide a Python Shell. Every time you type Python instructions into this window,
Python will follow your instructions and show you the result.
Input and Output
It’s traditional to start with Hello World! Type the following – take care to get the punctuation right:
print "Hello World!"
Try changing this to output your own choice of message.
Next, let’s make this a bit more personal – first by remembering a name in a variable:
name = "Fred"
print "Hello", name
Then by asking your name first:
name = raw_input("What's your name: ")
print "Hello", name
Programs often consist of:
Input
Processing – do something with the input
Output
The input and output may be numbers as well as words.
Processing may involve maths or making decisions.
Stephen Rice, April 2015
Coding in Python
Python can also compare numbers. For example:
print "Is 2 more than 1?", 2 > 1
print "Is 8 less than 3?", 8 < 3
Try changing the values and predict what the outcome will be. Then see if you’re right.
Python can see if two numbers are the same – let’s say your lucky number is 8:
lucky = 8
print "Is 13 my lucky number?", 13 == lucky
print "Is 8 my lucky number?", 8 == lucky
Take care to use one = sign when remembering the number and two == signs when checking it.
Python can also think of random numbers. This is a little bit trickier to type.
import random
random.randint(1, 6)
This is like rolling a die. Type the second line again to roll again. To save time you can click on the line you
typed before and press Enter to get it typed in again for you.
How do you think you would change the number of sides on your dice? Have a try.
Loops
Programs often do the same thing repeatedly; in fact sometimes they keep going forever until you make
them stop. Now try this:
while True:
random.randint(1, 6)
Press Ctrl+C to make it stop!
Stephen Rice, April 2015
Coding in Python
Decisions
We are now going to try making decisions. At this point you might want to, rather than have Python run
each line as you type it, write a number of lines and have Python run them all at once.
To do this go to File menu > New File and start typing code into the Editor window.
When you want Python to run your code, press F5.
The first time you run your code you will be asked to save the file – save it on your Desktop and name it
however you wish. Your output will appear in the Python Shell window. You can continue to click and type
code into the Python Shell but it could start to get a bit repetitive!
Leaving the maths for a moment, let’s make a (very short) adventure game.
Take care to type four spaces before each indented line.
print "You have two doors in front of you – which will you go through?"
choice = int(raw_input("Type a number: "))
if 1 == choice:
print "Door 1! A brave choice. Sadly you are devoured by a tiger."
elif 2 == choice:
print "You choose the second door. Unfortunately a bear greets you."
else:
print "A rule breaker eh! Best not to open doors at a zoo."
If you want you can try changing the words, adding extra doors, or changing which one is the safe choice.
You could even offer a further choice once you go through the first door. Ask for assistance if you want to
try this.
Going back to our eternal dice from the earlier, let’s combine a few of the things we’ve learnt to keep
rolling only until you get a six.
import random
while True:
roll = random.randint(1, 6)
print roll
if 6 == roll:
break
The last line tells Python to stop looping if you roll a six. Try this a few times and see how long it takes to
get a six. You could try changing this to look for a different number, or look for a number greater than 3 or
less than 4 or something else. See what effect this has.
If you’re feeling particularly confident, think about how to make the program run until you get two sixes, or
two in a row of any number.
Stephen Rice, April 2015
Coding in Python
Finally
Finally, you can put all you’ve done earlier together to make a “guess the number” game.
The brief for this game is it should:
Run forever unless stopped (or you win)
Think of a random number
Ask you to guess the number
If the guess is correct, output congratulations and stop looping
If the guess is wrong, go round again
o Optionally, show if the guess is too high or too low
Tips:
Discuss in your group what code you might use for each step in this program
Try writing out a plan for your code on paper first
Type each bit in turn and see if it works before you type the next bit
License
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/
Acknowledgements
http://knightlab.northwestern.edu/
http://missictteacher.com/
http://www.teachcomputerscience.com/
http://www.starteractivity.com/
http://www.codecademy.com/
Images from Microsoft Office
Stephen Rice, April 2015
© Copyright 2026 Paperzz