In this level you can repeat multiple lines of code with only 1 repeat command.
You do this by making a block of lines that you want to repeat.
The lines in this block will need indentation .
That means putting four spaces at the beginning of each line. You will also have to indent when you want to create a block of just one line.
repeat 5 times
print 'Hello everyone'
print 'This is all repeated 5 times'
In the next example you can have your fortune teller ask multiple questions and also print them!
Can you fill in right command on the blanks?
print 'I am Hedy the fortune teller!'
print 'You can ask me 3 questions.'
answers = yes, no, maybe
_ _ _
question = ask 'What do you want to know?'
print question
sleep
print 'My crystal ball says... ' answers at random
In this level you have to think carefully which lines of code should be in the block and which shouldn't. For example: If you want to sing the song the muffin man. You only want the line with 'the muffin man' to be repeated twice. This means the last line shouldn't start with indentation as it doesn't belong to the block. If you do start the last line with indentation the song will turn out wrong.
Each line in the example code starts with a blank. Remove the blanks and try to figure out which line need indentation and which don't to make the muffin man song.
_ print 'Do you know the muffin man?'
_ repeat 2 times
_ print 'The muffin man'
_ print 'Do you know the muffin man, who lives on Drury Lane?'
In a previous level you've programmed the song 'Bottles of beer'. But without the repeat command, you had to copy the verses many times.
In this level you can repeat the song 99 times, just by adding one simple line!
Add the right command on the blanks and indent the code correctly.
verse = 99
_ 99 times
print verse ' bottles of beer on the wall'
print verse ' bottles of beer'
print 'Take one down, pass it around'
verse = verse - 1
print verse ' bottles of beer on the wall'
Now that we can use the repeat command for multiple lines, we can make songs even more easily!
Finish the song of Brother John (Frère Jacques). Don't forget to use repeat!
print 'Brother John'
repeat 2 times
play C
play D
play E
play C
repeat 2 times
play E
play F
play G
sleep 1
You have learned to repeat a block of lines of code after a repeat command.
Now you can also use indentation to make blocks after a if or else command.
Check out the example code.
Add an else command to the example code. Make a block of line using indentation. You do this by starting each line with 4 spaces.
name = ask 'What is your name?'
if name is Hedy
print 'Welcome Hedy'
print 'You can play on your computer!'
In this level you can use multiple lines in your if commands, this way you can upgrade your happy or sad ending!
The example code shows two different endings; one where the characters jump in a time machine and one where they do not.
Complete the blanks with at least one sentence per ending.
Extra Make the story longer. What happens in the story? You can also add a second ask with different options.
Go back to your saved programs, choose your story program from level 5. Now write a good and a bad ending of at least three lines long each!
print 'OH NO! The T-rex is closing in!'
end = ask 'Do you want a happy or a sad ending?'
if end is happy
print 'Just in time Richard jumps back into the time machine!'
print _
else
print 'Oh no! Richard is too slow...'
print _
In this level you can make your virtual restaurant more elaborate by repeating multiple lines of code. Like this:
This code can be expanded with more items on the menu, for example offering drinks, and/or multiple courses or desserts. Add at least one more item. Extra Add even more items, as many options as you like!
print 'Welcome to Restaurant Chez Hedy!'
people = ask 'How many people will be joining us today?'
print 'Great!'
repeat people times
food = ask 'What would you like to order?'
print food
print 'Thank you for ordering!'
print 'Enjoy your meal!'
Now that we can repeat several lines, we can make figures more easily.
We only have to set the angle once and then use that variable in the repeat.
The example code creates a square. Change the code so that it create another figure, such as a triangle or a hexagon. This requires a change to two lines of code. Tip: An entire circle is 360 degrees.
Now create a drawing consisting of at least two polygons.
angle = 90
repeat 4 times
turn angle
forward 50
Extra We can now improve the program that draws different figures. Finish the code and you can draw any polygon you'd like!
figure = ask 'How many angles should I draw?'
angle = 360 / figure
repeat figure times
turn _
forward _
Recreate the drawings with the turtle!
Extra The number in brackets indicates in how many lines of code this figure can be drawn. Can you do it in the same amount of lines?
Debug this code. Good luck!
Warning! This code needs to be debugged!
answer = 'Welcome to our pizza shop. Would you like to order?'
if answer
amount 'How many pizzas would you like to buy?'
else
print "You're buying one anyways!"
amount = 1
'We have Margherita and Pepperoni pizzas.'
repeat amount
chosen_pizza = ask 'What kind of pizza would you like?'
print 'One ' chosen_pizza '.'
price = amount * 10
That will be ' price ' dollars please.'
Our ambulance is due for some service. The painted red cross on the side is fading and the siren is no longer working. Can you fix it?
The program should:
An error occurred.
An error occurred.
Success
Header
Waiting for a keypress...
Sleeping...
You must be logged in to hand in an assignment.