Exercise: Writing a Game
The main topics are:
- Variables
- Computer arithmetic
- Using and learning libraries
- Input and output
- Loops and choices
At the end we will have a Hangman game.
Variables
Python can remember things (variables -- think of algebra), so let's tell it to remember our secret word.
Create a script called
hangman.py on the PC using
gedit and type the following into it to assign the word
crocodile to
secret:
secret = 'crocodile'
You can tell it to print something by saying print. Add the following line:
print secret
When you run this you would see the word
crocodile printed on the Terminal.
You could add more
print secret lines and you would see one
crocodile printed for each line.
Python will remember what string you assigned to a variable until the script stops.
We aren't just restricted to strings, we can also remember numbers.
For example, add these two lines to your script:
number = 43
print number
Your overall script should now read:
secret = 'crocodile'
print secret
number = 43
print number
Copy it across to the phone:
push hangman.py
Now run it by tapping
hangman.py and selecting
Run with Terminal.
You should see the following, if you don't put your hand up to get some help.
Next