In this game below a code has been made to make sure the player can play on as long as they want... But the code is ineffective and way too long. Also, what if the player wants to play 101 games instead of 100? You can't play to infinity? In this level you will learn a command that makes all of this a lot easier!
game = 'on'
for i in range 1 to 100
if game == 'on'
answer = ask 'Do you want to continue?'
if answer == 'no'
game = 'over'
if answer == 'yes'
print 'Ok we will continue'
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'
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'
call song
print '🥳'
Recreate the drawings with the turtle!
Spiral
Spiral
distance = 5
while distance < 200
distance = distance + 5
_
Fan
Fan
define draw_a_square with side
_
i = 100
while i > 1
_ with 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.
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!'
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 = ask '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'
_
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 at random
number_2 = numbers at 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!'
Debug this random children's story. Good luck!
Warning! This code needs to be debugged!
names = 'Tanya', 'Romy', 'Kayla', 'Aldrin', 'Ali'
verbs='walking', 'skipping', 'cycling', 'driving', 'running'
locations = 'on a mountaintop', 'in the supermarket', 'to the swimming pool'
hiding_spots = 'behind a tree', under a table', in a box'
sounds = 'a trumpet', 'a car crash', 'thunder'
causes_of_noise = 'a television', 'a kid with firecrackers', 'a magic elephant', 'a dream'
chosen_ name = names at random
chosen_verb = verbs at random
chosen_location = 'locations at random'
chosen_sounds = noises at random
chosen_spot = hiding_spots random
chosen_causes = causes_of_noise at random
print chosen_name ' was ' chosen_verb ' ' chosen_location
print 'when they suddenly heard a sound like ' sounds at random
print chosen_name ' looked around, but they couldn't discover where the noise came from'
print chosen_name ' hid ' chosen_spot'
print 'They tried to look around, but couldn't see anything from there'
hidden = 'yes'
while hidden = 'yes'
print chosen_name 'still didn't see anything'
answer = ask 'does ' chosen_name ' move from their hiding spot?'
if answer = 'yes'
hidden == 'no'
print 'chosen_name moved from' chosen_spot
print 'And then they saw it was just' chosen_cause
print chosen_name 'laughed and went on with their day'
print The End
An error occurred.
An error occurred.
Success
Header
Waiting for a keypress...
Sleeping...
You must be logged in to hand in an assignment.