1 of 20

Reading Python Code #03 loops

CHG

2 of 20

Remember...

Loops start with a “while”

There is a second type, we are not looking at that for now.

We want to repeat a set number of times.

We generally DON’T want the loop to run infinitely (when do we?)

Conditions follow the same rule as if statements.

3 of 20

Key Ideas

Same conditions as if statements.

Use break if you want to get out.

While(True) generally isn’t great but it’s easier to learn.

4 of 20

Part 1

Basic Python Code

Try to see what the code does or write it in Spyder and run it.

You could also just explain what it does as well…

See what Happens

Yes…Mr Chuang made it easier to start with.

5 of 20

Example

What gets printed out into the console?

6 of 20

Q1

What gets printed out into the console?

7 of 20

Q2

What gets printed out into the console?

8 of 20

Q3

What gets printed out into the console?

9 of 20

Q4

What gets printed out into the console?

10 of 20

Q5

What gets printed out into the console?

11 of 20

Part 2

Here are coding exercises.

Make sure to include the code, not the output of the program!

12 of 20

Guess the Animal - Fox

Ask the user to guess the animal “Fox”. Keep asking them until they get it

YOUR ANSWER HERE

Hint. Delete to show.

13 of 20

Secret Password - Hunter12

Ask the user for a password.

If they get it wrong 3 times, break and say too many tries.

If they got it right, say logged in.

YOUR ANSWER HERE

Hint. Delete to show.

14 of 20

Valid username.

Ask the user for a valid username.

Valid usernames must have 3 or more characters.

Keep asking them until you get a valid one.

YOUR ANSWER HERE

Hint. Delete to show.

15 of 20

Sum to 10

Write code to add up all the numbers from 1 to 10

Hint: You should get 55

YOUR ANSWER HERE

Hint. Delete to show.

16 of 20

Sum to 1+2+3+4+5+...+n

Ask the user to enter a number.

Add up all the numbers from 1 to that number.

Hint: You should get 5050 if they input 100

YOUR ANSWER HERE

Hint. Delete to show.

17 of 20

Get close to 15

The user gets a random number from 1 to 6.

Ask if they want to stop or get a new number.

Stop when they go over 15.

YOUR ANSWER HERE

Hint. Delete to show.

18 of 20

Part 3

Hot and Cold

19 of 20

Hot and Cold.

Create a game where a character is on a 2D plane with x and y coordinates. Accept wasd input to move him in the 4 directions and update his position.

Print out a special message when he lands on the goal square, for example (9,3) and end the game.

Extension 1: Count the number of moves the user takes.

Extension 2: Make the game end after 30 moves.

Challenge: Give clues such as hot/cold to the destination after each step. Could use a numerical scale of hot and coldness. If you do MATH you better know how to calculate it properly.

Challenge 2:Randomise the goal and start square each time. - you are free to skip this when testing

Challenge 3: What about hotter or colder? Give hints that compare to the previous location

Challenge 4:At this point you might as well use ASCII characters and draw a board, use a symbol for squares and a symbol for the character.

You can assume the board will only be 10x10, you don't need to go too crazy

You do not need arrays to do this task.

20 of 20

Code - in notes.