Reading Python Code #03 loops
CHG
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.
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.
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.
Example
What gets printed out into the console?
Q1
What gets printed out into the console?
Q2
What gets printed out into the console?
Q3
What gets printed out into the console?
Q4
What gets printed out into the console?
Q5
What gets printed out into the console?
Part 2
Here are coding exercises.
Make sure to include the code, not the output of the program!
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.
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.
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.
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.
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.
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.
Part 3
Hot and Cold
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.
Code - in notes.