Maybe you have tried using decimal numbers in your restaurant adventure. If you did, you probably noticed that Hedy didn't understand them yet and always rounded off. From this level on you can use decimal numbers.
burger = 5
drink = 2
total = burger + drink
print 'You have ordered a burger and a drink'
print 'That costs ' total ' dollars please'
Decimal numbers
So far, Hedy did not allow for decimal numbers like 1.5, but now we do allow that. Note that computers use the .
for decimal numbers.
print 'Two and a half plus two and a half is...'
print 2.5 + 2.5
Maths with words In this level you can also do addition with words like this:
a = 'Hello '
b = 'world!'
print a + b
All texts need to be in quotation marks
For this level on you will also have to use quotation marks when storing a text with =
:
name = 'Hedy the Robot'
print 'Hello ' name
All items in lists need quotation marks too Lists are texts, so they need quotation marks too. Mind that each single item on the list has quotation marks. This allows you to save two words as 1 item on the list, for example 'Black Widow'.
superheroes = 'Spiderman', 'Batman', 'Black Widow'
print superheroes at random
All text after if
comparisons need quotation marks too
name = ask 'What is your name?'
if name = 'Hedy the Robot'
print 'Hi there!'
Numbers don't need quotation marks
For numbers, you do not use quotation marks in the =
:
score = 25
print 'You got ' score
In this level you'll learn how to use functions. A function is a block of code you can easily use multiple times. Using functions helps us organize pieces of code that we can use again and again.
To create a function, use define
and give the function a name. Then put all the lines you want in the function in a indented block under the define
line.
Leave one empty line in your code to make it look nice and neat. Great job! You have created a function!
Now, whenever we need that block of code, we just use call
with the function's name to call it up! We don't have to type that block of code again.
Check out this example code of a game of Twister. The function 'turn' contains a block of code that chooses which limb should go where.
Finish this code by setting the 2 variables chosen_limb and chosen_color. Then, choose how many times you want to call the function to give the twister spinner a spin.
Improve your code by adding a variable called 'people'. Use the variable to give all the players their own command in the game. For example: 'Ahmed, right hand on green' or 'Jessica, left foot on yellow'.
sides = 'left', 'right'
limbs = 'hand', 'foot'
colors = 'red', 'blue', 'green', 'yellow'
define turn
chosen_side = sides at random
chosen_limb = limbs _
chosen_color = colors _
print chosen_side ' ' chosen_limb ' on ' chosen_color
print 'Lets play a game of Twister!'
for i in range 1 to _
call turn
sleep 2
From level 12 on, you will also have to use quotation marks in lists, before and after each item.
Add two predictions to the list
fortunes = 'you will slip on a banana peel', _
print 'I will take a look in my crystal ball for your future.'
print 'I see... I see...'
sleep
print fortunes at random
Use functions in your songs! As you can see in the example code, you can make a function for each line of Twinkle Twinkle Little Star. Once you've programmed the first three lines, all you have to do is call the functions in the order you want them played in.
Finish the song of Twinkle Twinkle Little Star. Then look back at all the songs you've programmed in the levels before, can you make those codes better and shorter using functions too?
define first_line
play C
play C
play G
play G
play A
play A
play G
sleep
define second_line
play F
play F
play E
play E
play D
play D
play C
sleep
define third_line
play G
play G
play F
play F
play E
play E
play D
sleep
call _
call _
call _
call _
call _
call _
In this song we can make it even easier to program 'if you're happy and you know it, clap your hands'. Because we can put all of the actions in a variable, check it out:
Can you add the right amount of indentation to each line to make the song play correctly? Hint: Not all lines need indentation.
_ actions = 'clap your hands', 'stomp your feet', 'shout Hurray!'
_ for action in actions
_ for i in range 1 to 2
_ print 'if youre happy and you know it'
_ print action
_ print 'if youre happy and you know it and you really want to show it'
_ print 'if youre happy and you know it'
_ print action
Songs contain a lot of repetition. We can capture it with a function!
Look at the example code with the function. Fill out the two lines so the full song is printed.
define twinkle
print 'Twinkle'
print _
call twinkle
print 'Up above the world so high'
print 'Like a diamond in the sky'
call _
From this level on, you can use decimal numbers to make your menu more realistic.
Can you think of a code to give your friends and family a 15% discount?
price = 0.0
food = ask 'What would you like to order?'
drink = ask 'What would you like to drink?'
if food is 'hamburger'
price = price + 6.50
if food is 'pizza'
price = price + 5.75
if drink is 'water'
price = price + 1.20
if drink is 'soda'
price = price + 2.35
print 'That will be ' price ' dollar, please'
In this level, you can make a calculator that works for decimal numbers.
Fill out the blanks to complete the calculator. Remember to use a period and not a comma for decimal numbers.
Create a new mathematics practice program, but now use decimal numbers. Create a list of numbers, choose two to multiple and let the player answer. And of course you have to validate the answer! Extra Increase the difficulty by adding lives: A player loses a life for a wrong answer and after three wrong answers the game ends.
number1 = ask 'What is the first number?'
number2 = ask 'What is the second number?'
answer = _
print number1 ' plus ' number2 ' is ' _
We can use functions to draw more complex figures with less code.
Fill the function so that three squares are created. If you want the image to look nicer, you can make the lines between the squares white.
The code can be made even shorter. Place the final lines into a repeat
so the figure remains the same.
Create your own drawing with different figures.
Change both the number of figures with the repeat
and the shape of the figures in the define
define square
repeat 4 times
turn _
forward _
call square
forward 50
call square
forward 50
call square
In this adventure you learn how to make a digital piggy bank.
Finish the code to calculate how much money you have and how long you need to save up to buy what you want! Extra Maybe you have already saved some money? Deduct that from the amount you will have to save up.
print 'The digital piggy bank'
wish = ask 'What would you like to buy?'
price = ask 'How much does that cost?'
saved = ask 'How much money have you saved already?'
allowance = ask 'How much pocket money do you get per week?'
to_save = price - saved
weeks = to_save / allowance
print 'You can buy a ' _ ' in ' _ ' weeks.'
In this adventure you can create your own super spy code. Encode a message that only the right agent can decipher. If the enemy tries to crack the code, they will get some false info to waste their time.
Make your own secret code for your superspy and return both parts only to the real spy.
Add a third component to the code, like a piece of clothing or an object.
name = ask 'What is your name?'
if name is _
a = 'Go to the airport '
else
a = 'Go to the train station '
password = ask 'What is the password?'
if password is _
b = 'tomorrow at 02.00'
else
b = 'today at 10.00'
print _ _ _
Recreate the drawings with the turtle!
Firstly, define a function for each shape you want to use on the bracelet. Then, add the shapes to the bracelet like this:
Bracelet Designing program
Hint Bracelet Designing program
define draw_a_square
_
color white
turn -90
forward 300
turn 180
for i in range 1 to 5
color gray
forward 100
shape = ask 'What kind of shape would you like next on the bracelet?'
chosen_color = ask 'In which color?'
color chosen_color
if shape = 'square'
call draw_a_square
Debug this code. Good luck!
Warning! This code needs to be debugged!
define greet
greetings = 'Hello', 'Hi there', 'Goodevening'
print greetings at random
define take_order
food = ask 'What would you like to eat?'
print 'One food'
drink = 'What would you like to drink?'
print 'One ' drink
more = ask 'Would you like anything else?'
if more is 'no'
print 'Alright'
else
print 'And ' more
print 'Thank you'
print 'Welcome to our restaurant'
people = ask 'How many people are in your party tonight?'
for i in range 0 to people
call greet_customer
An error occurred.
An error occurred.
Success
Header
Waiting for a keypress...
Sleeping...
You must be logged in to hand in an assignment.