We use the < to check if the first number is smaller than the second number.
We use the > to check if the first number is bigger than the second number.
We use the == to check if two things are the same.
We use the != to check if two things are not the same.
We use the <= to check if the first number is smaller than or equal to the second number.
We use the >= to check if the first number is bigger than or equal to the second number.
Introduction
is
Guess my number
music
Haunted House
functions
Draw it!
Calculator
Calculator 2
Piggy Bank
Quizmaster
debugging
Quiz
With the example code you can calculate if you've passed a subject at school (so, a grade of six or higher).
You can see this code is extremely inefficient, due to the very long code in line 5.
All the different grades from 1 to 5 had to be programmed separately. Lucky for you, in this level you'll learn how to do this without this extremely long code!
first_grade = ask 'What score did you get on your first test?'
second_grade = ask 'What score did you get on your second test?'
added = first_grade + second_grade
mean_grade = added / 2
if mean_grade = 1 or mean_grade = 2 or mean_grade = 3 or mean_grade = 4 or mean_grade = 5
print 'Oh no! You have failed the subject...'
else
print 'Great! You have passed the subject!'
We are going to learn more new items. You might know them already from mathematics, the < and >.
The < checks if the first number is smaller than the second, for example age < 12 checks if age is smaller than 12.
If you want to check if the first number is smaller or equal to the second, you can use <=, for example age <= 11.
The > checks if the first number is bigger than the second, for example points > 10 checks if points is larger than 10.
If you want to check if the first number is bigger or equal to the second, you can use >=, for example points >= 11.
You use these comparisons in an if, like this:
age = ask 'How old are you?'
if age > 12
print 'You are older than I am!'
age = ask 'How old are you?'
if age < 12
print 'You are younger than me!'
else
print 'You are older than me!'
From this level on, if you want to compare exactly, you can use two equal signs. This is what most programming languages do:
name = ask 'What is your name?'
if name == 'Hedy'
print 'You are cool!'
You can also compare if something is not equal to something else using != like this:
name = ask 'What is your name?'
if name != 'Hedy'
print 'You are not Hedy'
In this level you can program the game 'Guess my number'
Exercise
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 at random
game = 'on'
for i in range 1 to 10
if game == 'on'
guess = ask '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'
You can program music for fun, but you can also use the musical notes to make something useful like a fire alarm!
Exercise
Make sure the fire alarm rings when there is a fire!
define fire_alarm
print 'FIRE!'
note = 40
for i in range 1 to 100
if note _ 50
note = note + 5
play _
else
note = 40
fire = ask 'Is there a fire?'
if fire _ 'yes'
call fire_alarm
Exercise
In this level you can use the < and > symbol to introduce lives to your game.
Make sure the player loses a life when they come across the wrong monster and that the game stops if you have no lives left.
print 'Escape from the haunted house'
lives = 3
doors = 1, 2, 3
monsters = 'the wicked witch', 'a zombie', 'a sleeping 3 headed dog'
for i in range 1 to 10
if lives _
good_door = doors at random
monster = monsters at random
chosen_door = ask 'Which door do you choose?'
if good_door == chosen_door
print 'You have chosen the correct door'
else
print 'You see...' monster
if monster == 'a sleeping 3 headed dog'
print 'Pffieuw.... Its asleep'
else
print 'You lose one life'
lives = _
else
print 'GAME OVER'
In the previous levels you have learned to create functions and use arguments with them. Another great use of a function is to let it calculate something for you.
You can give the function a calculation and it will give you the answer of the calculation. This answer is called a return value.
For example, in this code the function calculate_new_price will calculate the new price of any item. It will give you the new price as a return value.
Exercise
Finish this code. We have already made the variable new_price for you, you only need to set it.
You should finish the line of code by calling the function that calculates the new price.
define calculate_new_price with amount, percentage
percentage = percentage / 100
discount_amount = amount * percentage
return amount - discount_amount
old_price = ask 'How much is on the price tag?'
discount = ask 'What percentage is the discount?'
new_price = _ calculate_new_price with old_price, _
print 'The new price is ' new_price ' dollar'
Exercise
Create a program that asks the player how many corners their figure should have and then creates that figure.
The figure in the image is the output when the player fills in 10.
define calculate_degrees with amount_of_corners
_ 360 / amount_of_corners
define draw_figure with degrees
_
forward 400/amount_of_corners
turn _
amount_of_corners = ask _
degrees = call _ with _
call _ with
call _ with
In this adventure you will build a calculator that calculates your mean grade for you. If you get your calculator to work, you can move on to the next adventure, which allows you to add two extra features.
Exercise 1
Fill in the blanks to get the calculator to work.
Start with the fourth line, add a question to figure out what grade the student got.
In the fifth line you'll want to calculate the total of all grades, so the total = total + grade.
Then we get to set the return value. We want to return the mean, so the total divided by the amount of tests (4).
Lastly we finish the code by calling the function in line 8.
Did you get it? Awesome! Would you like to add even more to your calculator? This adventure continues in the next adventure!
define calculate_mean_grade
total = 0
for i in range 1 to 4
grade = ask _
total = total + _
return _ / 4
mean_grade = call _
print 'Your mean grade is ' mean_grade
Exercise 2
This is the second part of this adventure. The adventure starts in the previous adventure.
Of course, you don't always want to calculate the mean of 4 tests. You might want to calculate the mean of 10 tests or only 2...
We can fix this problem by adding the argument and variable 'amount_of_tests'.
Start a new line on line 3. Set the amount_of_tests argument by asking the student how many tests they have made.
Change the 4 in line 4 to the new argument amount_of_tests.
Lastly, change the 4 in line 6 to amount_of_tests
Try out your new program. Does it work?
Exercise 3
Did you want to make your program even better? Great! In the previous program you could only calculate the mean grade of 1 subject, but it would be better if you could calculate the mean grade for all subjects you want!
We won't tell you how to do it, but we will give you one tip: Start your code in line 1 with: define calculate_mean_grade with subject.
# Use your own code from the previous adventure.
Exercise
In this level you can let Hedy tell you if you have saved up enough money!
Finish this code by filling in the blanks!
_ calculate_budget with wish, money, allowance
to_save = wish - money
weeks = to_save / allowance
if wish _ money
print 'You need to save up some more!'
print 'Youll need ' weeks ' more weeks.'
else
print 'Great! You have enough'
print 'Lets go shopping!'
money = ask 'How much money have you saved?'
wish = ask 'How much money do you need?'
allowance = ask 'How much pocket money do you get each week?'
call _
Exercise
In this adventure you can make your own quiz! Fill in the blanks, add more questions and enjoy your own quiz!
You can make a quiz about anything you like: your hobby, your favorite animal, your favorite book or anything at all!
print 'Make your own quiz'
points_a = 0
points_b = 0
print 'Question'
print 'Answer option A'
print 'Answer option B'
answer = ask 'Which answer?'
if answer == 'A'
points_a = points_a + 1
if answer == 'B'
points_b = points_b + 1
print 'End of the quiz!'
print 'Lets see the results!'
if points_a > points_b
print 'You belong to the A club'
if points_b > points_a
print 'You belong to the B club'
Exercise
Debug this code. Good luck!
Warning! This code needs to be debugged!
define calculate_heartbeat
print 'Press your fingertips gently against the side of your neck'
print '(just under your jawline)'
print 'Count the number of beats you feel for 15 seconds'
beats == ask 'How many beats do you feel in 15 seconds?'
heartbeat = beats*4
print 'Your heartbeat is ' heartbeat
if heartbeat >= 60 or heartbeat <= 100
print 'Your heartbeat seems fine'
else
if heartbeat > 60
print 'Your heartbeat seems to be too low'
if heartbeat < 100
print 'Your heartbeat seems to be too high'
print 'You might want to contact a medical professional'
measure_heartbeat = ask 'Would you like to measure your heartbeat?'
if measure_heartbeat = 'yes'
call measure_heartbeat
else
'no problem'