Great job! You've reached another new level! In the previous level you've learned to use multiple lines of code in an if
or repeat
command. But you can't yet combine the two...
Good news! In this level you will be allowed to put an if
inside an if
, or inside a repeat
command. Putting a block of code inside another block of code is called nesting.
answer = ask 'Are you ready to learn something new?'
if answer is yes
print 'Great! You can learn to use the repeat command in the if command!'
print 'Hooray!'
print 'Hooray!'
print 'Hooray!'
else
print 'Maybe you should practice some more in the previous level'
Great job! You've reached another new level! In the previous level you've learned to use multiple lines of code in an if
or repeat
command.
But you can't yet combine the two...
Good news! In this level you will be allowed to put an if
inside an if
, repeat
inside a repeat
command and in each other.
Give it a try!
repeat 3 times
order = ask 'What would you like to order?'
if order is pizza
print 'Yammie'
else
print 'pizza is better!'
In this level you can also put an if
command inside another if
command.
continue = ask 'Do you want to continue?'
if continue = yes
sure = ask 'Are you sure?'
if sure is yes
print 'We will continue'
else
print 'You are not sure'
else
print 'You do not want to continue'
In this level you can program the whole rock, paper, scissors game by nesting the if
commands.
Can you finish the code? The program must tell who has won for every combination.
Extra Want to play more than one game? Expand the code so that you can play multiple rounds. You can even use an ask
to ask the user how many rounds they want to play.
choices = rock, paper, scissors
your_choice is ask 'What do you choose?'
print 'You choose ' your_choice
computer_choice is choices at random
print 'The computer chooses ' computer_choice
if computer_choice is your_choice
print 'Tie'
if computer_choice is rock
if your_choice is paper
print 'You win!'
if your_choice is scissors
print 'You lose!'
# finish this code
In this level you can use if
and repeat
commands inside other if
and repeat
commands.
This gives you many options and really helps you to make your story interactive.
Finish the code so the if
works correctly.
Add an if
and else
for the part of the story where Robin goes home too.
Go back to your level 8 story and use at least two if
s inside another if
.
print 'Robin is walking downtown'
location = ask 'Is Robin going into a shop, or does she go home?'
if location is shop
print 'She enters the shop.'
print 'Robin sees an interesting looking book'
book = ask 'Does Robin buy the book?'
if book is yes
_ print 'Robin buys the book and goes home'
_ else
_ print 'Robin leaves the shop and goes home'
else
print 'Robin goes home'
In a previous level, you've created a calculator. In this level, you can expand that code so it asks multiple questions.
Can you finish line 10 to get the code to work?
Give the player feedback when they enter an answer, like print 'Correct!'
or print 'Wrong! The correct answer is ' correct_answer
.
score = 0
repeat 10 times
numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
number_1 = numbers at random
number_2 = numbers at random
correct_answer = number_1 * number_2
print 'What is ' number_1 ' times ' number_2 '?'
answer = ask 'Type your answer here...'
print 'Your answer is ' answer
if _ is _
score = score + 1
print 'Great job! Your score is... ' score ' out of 10!'
From this level on you can - among other things - use a repeat
command inside a repeat
command.
That makes songs like 'Happy birthday' even shorter!
Finish the song!
first_time = yes
repeat 2 times
repeat 2 times
play C
play D
play C
if first_time is yes
play F
play E
first_time is no
else
_
In this level you can use nesting to make your restaurant more realistic and more fun!
The indentation was removed in the example code. Can you figure out how much indentation each line needs in order for the code to work properly? If the customer orders pizza, Hedy shouldn't ask what sauce the customer wants.
Extra A restaurant does not stock all sauces. Make a list of available sauces and give a reply with each order whether you sell it.
Extra Pizzas have toppings. Ask customers what they want.
Extra Do customers want a drink? Ask them too!
print 'Welcome to Restaurant Chez Hedy!'
people = ask 'How many people will be joining us today?'
print 'Great!'
price = 0
repeat people times
_ food = ask 'What would you like to order?'
_ print food
_ if food is fries
_ price = price + 3
_ sauce = ask 'What kind of sauce would you like with your fries?'
_ if sauce is no
_ print 'no sauce'
_ else
_ price = price + 1
_ print 'with ' sauce
_ if food is pizza
_ price = price + 4
print 'That will be ' price ' dollar'
print 'Enjoy your meal!'
In this level you can use nesting, which allows you to make the haunted house even more interactive!
Now it's very hard to win this game, can you make it easier to win?
Change your code so it only has one wrong door and two correct doors instead of one correct door and two wrong ones?
Tip: This means changing the variable correct_door into wrong_door, and switching the if
and else
code.
And of course you may also change the story and make it your own. Change the monsters or make it a happy game show where you get a gift!
print 'Escape from the Haunted House!'
player = alive
doors = 1, 2, 3
monsters = zombie, vampire, giant spider
repeat 3 times
if player is alive
correct_door is doors at random
print 'There are 3 doors in front of you...'
chosen_door = ask 'Which door do you choose?'
if chosen_door is correct_door
print 'No monsters here!'
else
print 'You are eaten by a ' monsters at random
player = dead
else
print 'GAME OVER'
if player is alive
print 'Great! You survived!'
Now that you know how to combine statements, you can create a touch type tool with pressed
.
Finish the code. Each time a random letter should be chosen, which you have to press. You get a point for a correct press, and two points deduction for a wrong press. Extra Clear the screen after each letter, and show the user how many points they have scored.
points = 0
letters = a, b, c, d, e
repeat 10 times
letter = _ _ _
print 'Press the letter ' letter
if letter is pressed
_
_
_
Now that we can use a repeat
inside a repeat
, we can create more complex figures.
This code creates three black triangles, change that into five pink squares.
Extra Create a figure of your own choosing consisting of at least two different shapes types.
color black
repeat 3 times
repeat 3 times
forward 10
turn 120
color white
forward 50
color black
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?
Extra Give the player a choice which country they would like to see the flag of.
Hint for the nested squares:
colors = red, blue, orange, yellow, pink, purple, green, brown, black
distance = 120
repeat 5 times
_
Hint for the flags:
country = ask 'which country would you like to see the flag of?'
if country is 'the Netherlands'
color_1 = red
color_2 = white
color_3 = blue
Debug this code. Good luck!
Warning! This code needs to be debugged!
print 'Welcome to our sandwich shop'
amount 'How many sandwiches would you like to buy?'
repeat amount times
ask is ask 'What kind or bread would you like your sandwich to be?'
types_of_bread is white, wheat, rye, garlic, gluten free
if chosen_bread in types_of_bread
print 'Lovely!'
else
'I'm sorry we don't sell that'
topping is ask 'What kind of topping would you like?'
sauce is ask 'What kind of sauce would you like?'
print One chosen_bread with topping and sauce.
price = amount * 6
print 'That will be 'price dollar' please'
An error occurred.
An error occurred.
Success
Header
Waiting for a keypress...
Sleeping...
You must be logged in to hand in an assignment.