Congratulations! You have reached the last level of Hedy! The code you have created here can be copied to real Python environments like Replit or PyCharm, and you can continue learning there! Note however that Python can only read English commands, so if you have been using other languages, you will need to switch to English now.
We arrived at real Python code! That means we need to use parentheses with print
and range
from now on.
It also means you can use Hedy code from this level in any Python environment as long as you use the English commands. If you haven't until now, you can switch the toggle in the commands menu to do so.
print('Hello!')
for i in range(1, 10):
print('This is line ', i)
If you want to print more than one item, you need to separate them by commas.
temperature = 25
print('It is ', temperature, ' degrees outside')
name = 'Hedy'
print('My name is ', name)
The final change we will need to make to get Python code is changing ask
into input
.
print('My name is Hedy!')
name = input('What is your name?')
print('So your name is ', name)
Let's make functions the Pythons way! To define a function, we no longer use:
define name_function with argument_1, argument_2:
but we use:
def name_function(argument_1, argument_2):
.
If you don't want to use arguments, you just leave the space between the parentheses empty.
To call a function, we don't need the call
command anymore. You just type the name of the function.
def calculate_score(answer, correct_answer):
if answer == correct_answer:
score = 1
elif answer == '?':
score = 0
else:
score = -1
return score
answer = input ('Where can you find the Eiffel Tower?')
correct_answer = 'Paris'
score = calculate_score(answer, correct_answer)
print ('Your score is... ', score)
Lastly, we'll turn for i in range 1 to 5
into real Python code, like this:
for i in range(1,5):
print (i)
We are going to print another story, but now we have to use brackets with print
.
Create a story of at least 5 sentences. You don't have to use 'name' just yet.
print('Welcome to this story!')
We have already prepared an input
for you. First, use the name
variable in your story.
Then add a second ask
and use that variable as well.
Tip: Remember the commas in a print
between text and variables!
name = input("What's your name?")
print('Welcome to this story!')
In level 16 we made songs using lists. These programs however are no longer working properly in this level. The colons from level 17 and the brackets from level 18 still need to be added.
The Drunken sailor song is given as sample code, but not yet working. Can you make sure everything works again? To help you, we've put _ in the places of some errors.
Now also look up your Old MacDonald song from level 16, and correct it.
lines = ['what shall we do with the drunken sailor', 'shave his belly with a rusty razor', 'put him in a long boat till hes sober']
for line in lines _
for i in range 1 to 3 _
print _ line _
print 'early in the morning'
for i in range 1 to 3
print 'way hay and up she rises'
print 'early in the morning'
Even in this last level of Hedy we can make some music! Be careful of all the syntax that is needed now. Take a good look at how the functions are defined and called upon in the example code. Finish the song!
def line_1():
for i in range(1, 5):
play A
play D
play F
play A
def line_2():
for i in range(1, 5):
play G
play C
play E
play G
def line_3():
_
print ('The drunken sailor')
print ('What shall we do with the drunken sailor?')
line_1()
line_2()
line_3()
print ('Early in the morning')
Debug this Old MacDonald program from level 16. Good luck!
Warning! This code needs to be debugged!
animals = ['pig', 'dog', 'cow']
sounds = ['oink', 'woof', 'moo']
for i in range 1 to 3
animal = animals[i]
sound = sounds[i]
print 'Old MacDonald had a farm'
print 'E I E I O!'
print 'and on that farm he had a ' animal
print 'E I E I O!'
print 'with a ' sound sound ' here'
print 'and a ' sound sound ' there'
print 'here a ' sound
print 'there a ' sound
print 'everywhere a ' sound sound
An error occurred.
An error occurred.
Success
Header
Waiting for a keypress...
Sleeping...
You must be logged in to hand in an assignment.