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'
We are now going to learn and and or! If you want to check two statements, you don't have to use two ifs but you can use and and or.
If you use and, then both statements, left and right of the and need to be true. We can also use or. Then only one statement needs to be correct.
name = ask 'what is your name?'
age = ask 'what is your age?'
if name is 'Hedy' and age is 2
print 'You are the real Hedy!'
elif name is 'Hedy' or age is 2
print 'So close.. but you are not Hedy!'
else
print 'You are not Hedy!'
With the and command you can shorten your rock, paper, scissors code! Check out the example code.
Finish the code such that a winner is always decided on. Run your code a few times to verify there is always a winner printed.
options = 'rock', 'paper', 'scissors'
your_choice = ask 'What do you choose?'
computer_choice = options at random
print 'You choose ' your_choice
print 'The computer chooses ' computer_choice
if computer_choice is your_choice
print 'Tie'
elif computer_choice is 'rock' and your_choice is 'paper'
print 'You win!'
elif computer_choice is 'rock' and your_choice is 'scissors'
print 'The computer wins!'
_
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 ifs 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'
By using the and and or commands, you can make your stories more versatile. You can ask two questions and respond to the combination of answers.
Look at the example code and finish it. Then add at least 2 more if codes with and or or.
Find a story from a previous level, and add one and or or.
example_code: |
print 'Our hero is walking through the forest'
print 'The path splits two ways'
path = ask 'Which path should she choose?'
weapon = ask 'What weapon does she draw?'
if path is 'left' and weapon is 'sword'
_
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!'
Let's make the practice program a bit harder. The player now has to answers two questions correctly. Fill out the blanks to complete the program.
Sometimes, calculations have multiple correct answers. For example, 10 can be divided by 5 and by 2. So the question 'What number divides 10?' can be answered by 2 and by 5.
Ask for a calculation that has multiple correct answers, ask the player to answer it, and determine if it is correct using or.
Empty the programming field and create your own solution.
answer1 = ask 'What is 10 times 7?'
answer2 = ask 'What is 6 times 7?'
if _ _ _ _ _ _ _
print _
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
_
We can simplify the superspy code with and, such that we only need one if.
Complete the code by filling the right command on the blank. Tip: The superspy has to answer BOTH questions correctly, before they get the confidential information!
We want to confuse the enemy even more! Create a list with fake answers and select one at random when a wrong answer is given.
name = ask 'What is your name?'
password = ask 'What is your password?'
if name is 'Agent007' _ password is 'TOPSECRET'
print 'Go to the airport at 02.00'
else
print 'Go to the train station at 10.00'
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 will learn new commands to extend your code even further.
Place a and and a or in the logical place in the program.
Expand your restaurant with at least one more and and one or.
For example, create a special discount coupon that only applies to pizza, or give your customer a free drink
with fries and pancakes. Or something completely different of course!
price = 10
food = ask 'What would you like to eat?'
drinks = ask 'What would you like to drink?'
if food is 'sandwich' _ drinks is 'juice'
print 'That is our discount menu'
price = price - 3
if drinks is 'water' _ drinks is 'juice'
print 'That is a healthy choice'
print 'That will be ' price ' dollars'
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 of 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 dollars' please'
The staff at our emergency room has a problem. They say that there are a lot of patients coming in who don't really have an emergency and who should actually go to their gp. These people are filling up the waiting room and putting a lot of workload on our staff. Will you help the emergency room staff by making a triage app that tells the patient if they have to go to the emergency room or not?
Your program should tell the patient if they should 'go to the emergency room a.s.a.p.', 'call your healthcare provider to discuss your situation' or 'go to your own gp at earliest convenience'. The programm will ask these questions:
An error occurred.
An error occurred.
Success
Header
Waiting for a keypress...
Sleeping...
You must be logged in to hand in an assignment.