We are going to learn a new loop, the while loop! We continue the loop as long as the statement is true.
So don't forget to change the value in the loop.
In the example code, we continue until a correct answer has been given. If the correct answer is never given, the loop never ends!
answer = 0
while answer != 25
answer = ask 'What is 5 times 5?'
print 'A correct answer has been given'
Using the while loop can make your stories more interesting. For example, you can use while game == 'on' so you can play until the game is over.
Or you can use while sword == 'lost' so the player can't continue the game until they have found something.
The example code shows you how to use the while loop in a story. Now think of your own scenario in which the player has to find something before they can continue.
keys = 'lost'
print('You are standing in your garden and you have lost your keys.')
print('Where do you want to look for them?')
print('You can choose: tree, flowerbed, rock, postbox')
while keys == 'lost':
location = input('Where do you want to look?')
if location == 'flowerbed':
print('Here they are!')
keys = 'found'
else:
print('Nope they are not at the ', location)
print('Now you can enter the house!')
In this level you can create a little game in which you'll have to throw 6 as fast as possible. We have started the code, it's up to you to get the game to work!
Firstly, add a while loop that checks if 6 has been thrown or not.
As long as you haven't thrown 6 already, throw the dice on a random number.
Print what the player has thrown.
Add a try to the amount of tries
Wait a second before you throw again, or - in case you've thrown a 6 - before the game ends.
options = [1, 2, 3, 4, 5, 6]
print 'Throw 6 as fast as you can!'
thrown = 0
tries = 0
_
_
_
_
_
print('Yes! You have thrown 6 in ', tries, ' tries.')
Play until you beat the computer! But first, finish the example code...
won = 'no'
options = 'rock', 'paper', 'scissors'
while won == 'no'
your_choice = ask 'What do you choose?'
computer_choice = options at random
print 'you chose ' your_choice
print 'the computer chose ' computer_choice
if computer_choice == your_choice
print 'Tie!'
if computer_choice == 'rock' and your_choice == 'scissors'
print 'You lose!'
if computer_choice == 'rock' and your_choice == 'paper'
print 'You win!'
won = 'yes'
_
Warning This adventure can become extremely annoying!
We can also use the while command to repeat a song forever.
Finish the never-ending song.
define song
play _
yes_or_no = ask 'Do you want to hear my never-ending song?'
while yes_or_no = 'yes'
song()
print '🥳'
Recreate the drawings with the turtle!
Spiral
Spiral
distance = 5
while distance < 200
distance = distance + 5
_
Fan
Fan
define draw_a_square(side)
_
i = 100
while i > 1
_(i)
_
i = i - 3
Star
Star A star is usually drawn using 144-degree-turns. If you change this slightly to 143 degrees for example and repeat the pattern multiple times with a while loop you can make this figure.
You can add the while loop to the calculator game you've learned to make in a previous level.
This makes sure the player can't continue to the next question if they answer incorrectly.
Add the while loop in the function, ask the player what number_1 times number_2 is and print their answer.
Then call the function.
define new_question():
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
number_1 = numbers[random]
number_2 = numbers[random]
correct = number_1 * number_2
answer = 0
_
_
_
print('Well done!')
print('Give 10 correct answers to win!')
for i in range 1 to 10:
_
print('You win!')
With the while you can make sure your customers can keep adding orders until they are done.
Correctly add the while command to this code.
print 'Welcome at McHedy'
more = 'yes'
_
order = ask 'What would you like to order?'
print order
more = ask 'Would you like to order anything else?'
print 'Thank you!'
In this level you can program the game 'Guess my number'
Fill in the correct symbols on the blanks to get the game to work.
print('Guess my number')
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
number = numbers[random]
game = 'on'
for i in range (1, 10):
if game == 'on':
guess = input('Which number do you think it is?')
if guess _ number
print ('Lower!')
if guess _ number
print ('Higher!')
if guess _ number
print '(You win!')
game = 'over'
Debug this code. Good luck!
Warning! This code needs to be debugged!
define (food_order)
toppings = input('pepperoni, tuna, veggie or cheese?')
size = input 'big, medium or small?'
number_of_pizza = ask('How many these pizzas would you like?')
print('YOU ORDERED')
print(number_of_pizzas ' size ' topping ' pizza')
def drink_order:
drink = input'water, coke, icetea, lemonade or coffee?'
number_of_drinks = input('How many of these drinks would you like?')
print ()('YOU ORDERED')
print (number_of_drinks ' ' drink)
'Welcome to Hedy pizza'
more_food = input 'Would you like to order a pizza?'
while more_food = 'yes':
return more_food
more_food == input('Would you like to order a pizza?')
more_drinks = input('Would you like to order some drinks?')
while more_drinks == 'yes':
call drink_order
more_drinks == input('Would you like to order more drinks?')
print 'Thanks for ordering!'
Our medical students have to learn the medical terms for all the bodyparts. They seem to be struggling to remind all the anatomical names and they seem to find the studying very boring. Can you help them by creating a game that allows them to practise the medical terms? These are the terms that the students need to know: corpus = body, caput = head, ocolus = eye, nasus = nose, auris = ear, os = mouth, collum = neck, manus = hand, pectus = chest, humerus = shoulder, venter = stomach, tergum = back
The program should:
Congratulations! You have reached the last level of Hedy! The code you have created here can be copied to real Python environments like Replit or PyCharm, and you can continue learning there! Note however that Python can only read English commands, so if you have been using other languages, you will need to switch to English now.
An error occurred.
An error occurred.
Success
Header
Waiting for a keypress...
Sleeping...
You must be logged in to hand in an assignment.