The personal wiki of ...

Exercise: Writing a Game

The main topics are:

  1. Variables
  2. Computer arithmetic
  3. Using and learning libraries
  4. Input and output
  5. 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.

sl4a quickaction.png

You should see the following, if you don't put your hand up to get some help.

memory.png

Next
I Attachment Action Size Date WhoSorted ascending Comment
memory.pngpng memory.png manage 46 K 20 Jan 2012 - 03:39 Main.ian  
sl4a_quickaction.pngpng sl4a_quickaction.png manage 16 K 20 Jan 2012 - 03:35 Main.ian  
Contact Us | Section Map | Disclaimer | RSS feed RSS FeedBack to top ^

Valid XHTML and CSS | Built on Foswiki

Page Updated: 12 Dec 2012 by ian. © Victoria University of Wellington, New Zealand, unless otherwise stated