In this level we will grow a bit more towards real Python code. You will also learn how to match two lists together. This way you can program a code in which the correct animal is matched to the right sound. Because the two codes below... Are obviously nonsense!
animals = 'chicken', 'horse', 'cow'
sounds = 'cluck', 'neigh', 'moo'
for animal in animals
print 'A ' animal ' says ' sounds at random
You could also try to make it work this way, but....
animals = 'chicken', 'horse', 'cow'
sounds = 'cluck', 'neigh', 'moo'
for animal in animals
for sound in sounds
print 'A ' animal ' says ' sound
Note: These codes will not work like this in this level. Head to the next adventure to see which parts you need to correct.
We are going to make lists the Python way, with square brackets around the lists! We also keep the quotation marks around each item like we have learned in previous levels.
We use square brackets to point out a place in a list. For example: friends[1]
is the first name on the list of friends, as you can see in the first part of the example code.
The second part of the example code shows you that we can also match 2 lists using the variable i.
friends = ['Ahmed', 'Ben', 'Cayden']
print friends[1] ' is the first friend on the list.'
print friends[2] ' is the second friend on the list.'
print friends[3] ' is the third friend on the list.'
#now we will match 2 lists using the variable i
lucky_numbers = [15, 18, 6]
for i in range 1 to 3
print friends[i] 's lucky number is ' lucky_numbers[i]
Now that you've learned to use the brackets in lists, you can also start using the at random command in the Python way!
You simply type the name of your list with [random]
behind it!
fruit = ['apple', 'banana', 'cherry']
print fruit[random]
This haunted house game uses the connection between the lists you can use in this level.
For example: all the properties that belong to the zombie are first in all the lists, witch second and vampire third.
Check out the code and fill in weapons[i]
, monsters[i]
, bad_fate[i]
, good_fate[i]
, hint[i]
on the correct blanks to get the code to work!
numbers = [1, 2, 3]
i = numbers[random]
hint = ['growling', 'a cackling laugh', 'fluttering batwings']
monsters = ['zombie', 'witch', 'vampire']
bad_fate = ['Your brain is eaten', 'You are forever cursed', 'You are bitten']
good_fate = ['You throw the ham. The zombie is distracted and starts eating it.', 'You set the curtains on fire. The witch flees out of fear for the fire', 'The vampire hates garlic and flees']
weapons = ['ham', 'lighter', 'garlic']
print 'You are standing in front of an old mansion'
print 'Something is not right here'
print 'You hear ' _
print 'You are going to explore it'
print 'You enter the kitchen and see a lighter, a raw ham and a garlic.'
your_weapon = ask 'What do you bring with you?'
print 'With your ' your_weapon ' you enter the living room'
print 'There you find a ' _
needed_weapon = _
if your_weapon == needed_weapon
print 'You use your ' your_weapon
print _
print 'YOU WIN!'
else
print 'You have chosen the wrong weapon...'
print _
print 'GAME OVER'
In this level, you can program a song like OldMacDonald even more quickly. You can connect the right animal to the right sound by simply putting them in the same place in the list. The Drunken Sailor is also quickly made in this level. You only need 8 lines for the entire song, check it out!
Complete the Old MacDonald song by setting the variable animal
to animals[i]
and sound
to sounds[i]
.
animals = ['pig', 'dog', 'cow']
sounds = ['oink', 'woof', 'moo']
for i in range 1 to 3
animal = _
sound = _
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
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'
Finish the nursery rhyme!
number = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
object = ['on his drum', 'on his shoe', 'on his knee', 'on his door', 'on his hive', 'on his sticks', 'up in heaven', 'on his gate', 'on his vine', 'once again']
_
print 'This old man'
print 'He played ' _
print 'He played knick-knack ' _
print 'With a knick-knack paddywhack'
print 'Give the dog a bone'
print 'This old man came rolling home'
sleep 8
clear
Now create your own code for the nursery rhyme 'The wheels on the bus' on the same way!
object = ['wheels', 'doors', _]
movement = [ 'round and round', 'open and shut', _]
Upgrade your Old MacDonald code!
Take your code from the 'Sing a Song' adventure and add musical notes to it! You can make a function for each line in the song and call that function after the line is printed. We defined the first line for you and called it in the code. Can you finish the whole song?
define line_1
for i in range 1 to 3
play G
play D
for i in range 1 to 2
play E
play D
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'
call line_1
print 'E I E I O!'
_
Take a look at the example code. This is a program to practise French vocabulary. Now make your own program to practice your vocabulary in a new language. If you don't know any other languages, you can use Google translate or you can use emojis and your native language.
french_words = ['bonjour', 'ordinateur', 'pomme de terre']
translation = ['hello', 'computer', 'potato']
score = 0
for i in range 1 to 3
answer = ask 'What does ' french_words[i] ' mean?'
correct = translation[i]
if answer == correct
print 'Correct!'
score = score + 1
else
print 'Wrong, ' french_words[i] ' means ' translation[i]
print 'You gave ' score ' correct answers.'
Let's program a game of tic-tac-toe!
In this adventure we'll start with creating an empty field.
Create a list called field This list will be our playing field. This list is filled with 9 dots, since there are no x's and o's yet at the start of our game.
Create a function that prints the field Firstly, clear the screen so the old playing fields will be removed. Then we print the first line of our Tic Tac Toe field. This line consists of the first 3 spots in our list field. We have already programmed this line for you. Now finish the field by printing spot 4, 5, and 6 on the second row and spot 7, 8 and 9 in the third row.
Call the function that prints the field Now call the function.
Continue in the next adventure In the next adventure you'll learn how to program the game itself.
# Create a list called field
_ = ['.', '.', '.', '.', '.', '.', '.', '.', '.']
# Create a function that prints the field
define print_field
_
print 'TIC TAC TOE'
print field[1] field[2] field[3]
_
_
# Call the function
In the previous adventure you've learned how to create a playing field. Now you'll learn how to create the game!
Paste your code Start by pasting your code from the previous adventure here.
Add variables Underneath your list called field
we'll add 2 more variables that we'll need to program the game.
The variable game_over
tells us if the game is over, and should be 'no' at the start of the game.
The variable sign
tells us if it's the turn of player x or player o. Set the variable to 'x'.
The game First use a while
command, to make sure the game keeps on playing the variable while game_over is set to no.
During the game, we first ask the player which spot they choose. Then we change the field with the number they chose into their sign.
Then we print the field again and we ask the player if they've won yet. Lastly we want to switch whose turn it is, so if the sign is 'x' it should be 'o' and the other way around.
Test your game Does your game work? Great, have fun playing the game! If not, use the ladybug button to debug your code. You might have noticed one mistake in the code though, you can steal the other player's spot! If the other person chose spot 1, you could simply enter 1 after them and steal their spot. That's not fair! Go to the next adventure to learn how to fix this problem.
# Paste your code from the previous adventure here
# Add variables
game_over = _
sign = _
# The game
while _
choice = _ 'Player ' sign '_?'
field[choice] = _
_ print_field
game_over = ask _
if sign = 'o'
sign = _
_
sign = _
You might have noticed one mistake in the code you've made in the previous adventure. You can steal the other player's spot! If the other person chose spot 1, you could simply enter 1 after them and steal their spot. That's not fair! In this adventure we'll fix that mistake.
Paste your code here Paste your code from the previous adventure here.
Fix the mistake To fix the mistake we replace the line that says field[choice] = sign
. This turns any spot that the player has chosen into their sign.
Go to the header that says 'Use this to fix the mistake' and finish the code. We first want to check if the chosen spot is still empty, so if field[choice] = '.'
. If that is the case, you are allowed to take it.
Then we make an else command and print 'Sorry, this spot is already taken' if the spot is not empty. Lastly, we add a sleep
command, so the players can actually read the text before it gets cleared again.
Now copy this piece of code and replace the line field[choice] = sign
with this new piece of code.
Play your game! Now the game should work properly! Good job! The only flaw is that you can get a bit annoyed that the game keeps asking you if you've won yet. Do you want to fix that? Go to level 17 and we'll fix it!
# Paste your code here
# Use this to fix the mistake
if _ = '.'
field[choice] = sign
else
print _
_
Let's make a game of Simon Says! Simon Says is a memory game in which the player will be given a color. They have to repeat that color back. If they get it right a color is added to the sequence, so they now have to remember 2 colors, then 3, then 4 etc. the game stops as soon as the player makes a mistake.
In this first part of the Simon Says adventure, we'll let the computer pick a random color and add it to a list.
Make 2 lists First, make a list called colors
and fill it with the colors red, yellow, green and blue.
Then make a list called simon_sequence
. This list will be used as the answer.
At the start of the game this lists need to be empty. unfortunately, we can't create an empty list (yet), so we'll fill it with the words 'empty' and 'list' and we'll remove them from the list immediately.
Create a function that adds a color to the sequence Now that we have an empty list called simon_sequence, we can start filling it with random colors.
We do that with a function, so we can call it everytime there's a new level in our game. Create a function called add_random_color
.
Then create the variable random_color and set it to a random color. Next, add this random color to the simon_sequence.
Create a function that shows the simon_sequence Start by naming the new function show_simon_sequence
with level
as an argument. Now we want to show as many colors as the level we are in (in level 1 you see 1 color, in level 2 you see 2 colors etc).
So we repeat level
times, to print the simon_sequence[i]
. Each time a color is shown, wait for 1 second and then clear the screen.
Test your program Before you go to the next level, test if the functions are working by calling both of the functions. If they're working you should see a random color in your output screen. Remove this testing part of your code, copy the code and continue to the next adventure to learn more about the simon says game!
# Make 2 lists
colors = _
_ = ['empty', 'list']
remove _ from simon_sequence
remove _
# Create a function that adds a color
_ add_random_color
_
add _
# Create a function that shows the simon_sequence
define _
for i in range 1 to _
print _
_
_
# Test your program
call _
call show_simon_sequence with 1
We'll continue with our Simon Says game!
Paste your code here Paste your code from the previous level here. Don't forget to remove the part that was just used for testing the functions.
Create a function that creates the player_sequence The list player_sequence
is used to capture the answers of the player. First we define the function with the argument level.
Next, we ask level times what the color is that they choose. We call that variable answer
. Then we add the variable answer
to the list player_sequence.
Setting up the game Before we program the game in the next adventure, we'll need some starting variables. First, we'll set the variable level
to 1 and the variable game_over
to False.
Then we make an introduction for the game. We'll print 'Welcome to Simon Says!' and clear the screen after 1 second.
Continue to the next adventure to finish the game! Don't forget to copy your code and take it with you to the next adventure.
# Paste your code here
# Create a function that creates the player_sequence
define _
for _
_ 'What is color number ' i '?'
add answer to _
# Set up
level = _
game_over = _
print _
_ 1
_
In this adventure we'll program the game of Simon Says!
Paste your code Copy your code from the previous adventure and paste it here.
Program the game We start by making sure the game goes on while the game isn't over. Then we print what level the player is on, we use the variable level for that. We only show that for 1 second and then we clear the screen again.
Now, we have to create the empty list player_sequence. We've already programmed how to fill the list, with our function player_sequence
, but we never made the list itself. To create the list we use the same trick as we did in the previous tab.
We'll make a list with the words 'empty' and 'list' on it, and then we remove both these words. Next, we'll call all of the 3 functions that we've created.
Lastly, we'll have to check if the player gave the correct answers (so if the player_sequence and the simon_sequence are the same).
If that's the case, we'll compliment the player. Wait for 1 second and increase the level with 1.
Did the player give the wrong answer, we'll tell them and end the game by setting game_over to 'True'
Enjoy your game! Great job! Does your game not work? Use the ladybug button to debug your code!
# Paste your code here
# The game
while game_over _
print _
_
_
_ = ['empty', 'list']
remove _
remove _
call _
call _ with _
call _ with _
if player_sequence == _
_
_
_
else
_
game_over = _
Debug this code. Good luck! Tip: Make sure that you only see your score once in the end.
Warning! This code needs to be debugged!
country = ['The Netherlands', 'Poland', 'Turkey', 'Zimbabwe', 'Thailand', 'Brasil', 'Peru', 'Australia', 'India', 'Romania' ]
capitals = 'Amsterdam', 'Warsaw' 'Istanbul', 'Harare', 'Bangkok', 'Brasilia', 'Lima', 'Canberra', 'New Delhi', 'Bucharest'
score = 0
for i in range 0 to 10
answer = ask 'What's the capital of ' countries[i]
correct = capital[i]
if answer = correct
print 'Correct!'
score = score + 1
else
print 'Wrong,' capitals[i] 'is the capital of' countries[i]
print 'You scored ' score ' out of 10'
An error occurred.
An error occurred.
Success
Header
Waiting for a keypress...
Sleeping...
You must be logged in to hand in an assignment.