Friday, 13 June 2014

While I make a decison

Learning aims:

To be able to do something repeatedly.
To be able to get the computer to make decisions

Introduction

Remember playing blockly?
Python has two simmilar commands - while and if.

'While' is similar to the 'repeat until' command.  Whatever is in the block will run while something is true (or not False'.

'if' is similar to the 'if path' statement in blockly.  it tests something, then if it is True, then it will do what is in the if block.

While commands do something until something is no longer true.

Task 1

We are going to create a game - Not a very good game - but you can improve it later.
code 1
Enter game1.py into your browser to play it you will need to run it from a terminal - to open a terminal - click on view >> new terminal.

points to note - line 9 on the code above is wrapped - (guesses)) is still on line 9 not on line 10.
Notice how the indents tell the computer what code belongs in each block.  lines 9-21 are all in the while block on line 8.  lines 11 and 12 belong in the if block on line 10. 

When running this we should get into the habit of running the code in the terminal.

Questions


  1. What variables are used in this code - what do they do?
  2. How does the program know when to stop the while loop?
  3. What word describes the data stored in the playing variable (True or False) (this might need looking up)
  4. How does the while loop work?  
  5. Which lines are run in the while loop which starts on line 8?
  6. How does the if statement work?  
  7. Which lines are in the if block that starts on line 19?
Write up your answers in a word document and hand in via the moodle.

Task 2

Make this a better game by making the computer choose a random number between 1 and 100.  You may need to look at  https://docs.python.org/2/library/random.html for help.  Follow Mr Jones in Github to hand This in.

No comments:

Post a Comment