Now we are going to change indentation a little bit. Every time that we need an indentation, we need :
at the line before the indentation.
In this level you can also use a new command: elif
. elif
is short for else if
and you need it when you want to make 3 (or more!) options.
Check it out!
Now we are going to change indentation a little bit. Every time that we need an indentation, we need :
at the line before the indentation.
for i in range 1 to 10:
print i
print 'Ready or not, here I come!'
In this level you can also use a new command: elif
. elif
is a combination of the keywords else
and if
and you need it when you want to make 3 (or more!) options.
Check it out!
prizes = ['1 million dollars', 'an apple pie', 'nothing']
your_prize = prizes[random]
print 'You win ' your_prize
if your_prize == '1 million dollars' :
print 'Yeah! You are rich!'
elif your_prize == 'an apple pie' :
print 'Lovely, an apple pie!'
else:
print 'Better luck next time..'
You can use the elif to create different options.
Firstly, add colons to get the code to work. Then finish this code by adding at least 2 other songs for other moods. For example a happy song and an angry song.
define scary_song
for i in range 1 to 3
play G
play E
sleep 2
for i in range 1 to 3
play F
play D
mood = ask 'Which emotion are you feeling?'
if mood is 'fear'
call scary_song
elif _
In the previous level you've learned how to make a tic-tac-toe game. The game works, but is quite annoying as it keeps asking you if you've won yet.
Now that we have the elif
command, we can let the game decide if someone has won and it can stop asking us!
Paste your code Paste your code from the previous level here and make it level 17 proof. In this level you've learned to use a colon everytime you create a block of code. Please add the colons in the correct spots.
Create a function that detects if someone's won We have started the function for you, paste it under the function print_field
and finish the function. You can see that this first piece of code checks to see if spot 1, 2 and 3 are the same, because if they are you have 3 in a row.
It also checks if they are not a dot, because if they are, the line might have the same symbols on them, but that's just because it's still empty.
If all these conditions are met, the game is over and the winner is printed.
Finish this function with all possible ways to win. This means you have to make this for the other 2 rows, 3 columns and 2 diagonals.
If you've finished all the other options, the function should return the variable game_over
so we can use it in our game.
Call the function in the game Go to the line game_over = ask 'Did you win?'
and change it to game_over = call detect_winner with field, sign
. Now the function will check if there's a winner and the game doesn't need to keep asking anymore!
Enjoy your game! Great job! You have finished the game! Enjoy playing it!
# Paste your code here and make it level 17 proof
# Create a function that detects if someone has won
define detect_winner with field, sign:
if field[1] == field[2] and field[2] == field[3] and field[1] != '.':
game_over = 'yes'
print 'Player ' sign 'wins!'
elif:
_
else:
game_over = 'no'
return _
In this adventure we program a game of hangman. First we make some preparations, then we program the game and in the third part we add a drawing with the turtle.
Set the variables In this game of hangman, Player 1 chooses an answer and Player 2 has to guess the letters in this answer.
To let the computer know all the letters in the word, we will turn the answer into a list of letters. We also do this with the guesses Player 2 makes.
We will start the game with 2 empty lists. We have made an empty list for the variable answer for you. Now make an empty list for guessed_letters as well.
Then we fill in how many mistakes were made. At the start of the game, this should be 0.
The variable amount_letters
tells us how many letters are in the answer. Ask Player 1 to tell us how many letters their word has.
Lastly we tell the computer if the game is over. We use the variable game_over
and set it to False
.
Choosing the answer We want Player 1 to be able to choose the answer. We'll ask them, as many times as necessary, what the next letter is. Then we add that letter to the answer. Lastly, we add an empty _ to the list of guessed letters, so we get as many _s as there are letters in the answer.
Player 2's turn
Tell Player 2 its their turn. Then tell Player 2 how many letters there are in the answer. Finally, print the list of guessed_letters
.
Go to the next adventure Now that all the starting variables are set, we can start programming the game itself. Check out the next tab to learn how!
print 'Hangman!'
# Set the variables
answer = []
guessed_letters = _
mistakes_made = _
amount_letters = ask _
_ = 'False'
# Choosing the answer
for _
letter = ask 'Player 1, what is letter ' i '?'
_
add '_' to _
# Player 2 turn
print _
print _
print guessed_letters
Now it's time to program the hangman game.
Paste your code Copy your code from the previous adventure and paste the code in the programming field.
The game This game continues playing until Player 2 is game over. Fill in the while
command accordingly. Now, Player 2 is allowed to guess a letter, so ask Player 2 to guess a letter.
We need to check if their answer is correct, so check if their guess
is (somewhere) in the (list) answer
. Then we let the computer figure out which of the letter(s) is the guess. We have already programmed that part for you.
Next, we want to compliment the player for finding a correct letter and we want to print the list guessed_letters
, so the player can see their progress.
The next part we're going to program is what happens when the player has guessed all of the letters. So, if their list of guessed_letters
is the same as our list answer
.
If the lists are the same, congratulate Player 2 with their victory and set the variable game_over
to True
.
Next, we'll program what happens when Player 2 guesses wrong (so the else
command). First, tell the player that their guess was wrong. Then increase the mistakes_made
variable by 1.
For the last part we'll program what happens when Player 2 has made 10 mistakes. We'll print that Player 1 has won the game. Then we'll print the correct answer. And finally, we'll set our game_over
variable to True
, so the game stops.
Go to the next adventure Amazing work! Your game is playable, but wouldn't it be fun if the hangman was actually drawn when Player 2 makes a mistake?
# Paste your code here
# The game
while game_over _
guess = _
if _
for i in range 1 to amount_letters:
if answer[i] == guess:
guessed_letters[i] = guess
print _
if guessed_letters == _:
print _
game_over = _
else:
print _
mistakes_made _
if _ == 10:
print _
print _
_
In a game of hangman the mistakes are shown by drawing a part of the hangman each time a mistake has been made. We now add those drawings with our turtle!
Create a function that draws the hangman Create a function that draws the hangman in 10 steps. We have already made step 1 for you.
Test the function Test the function by calling the function with 10. If you are happy with the function, remove the line that calls the function for now. We will call the function when the player makes a mistake.
Paste your hangman game under your function Go back to the previous adventure and copy your hangman game. Paste the game underneath your function.
Call the function when the player makes a mistake Under the line mistakes_made = mistakes_made + 1
we will call the function. We want the turtle to take the same amount of steps as the player has made mistakes, so we call the function with mistakes_made
as argument.
Enjoy your game!
# Create a function that draws the hangman
define draw_hangman with step:
if step == 1:
color white
forward -100
turn 90
forward -50
color black
forward 100
forward -50
if step == 2:
_
# Paste your hangman game here
Blackjack is a simple game of cards in which you have to get as close to 21 points as possible. You get two cards. Each card is worth their numeral value, and the face cards (Jack, Queen and King) are worth 10 points. The Ace is worth either 1 or 11 points (you can choose). The dealer, your opponent, also gets two cards. If you want, you can get another card, and its points will be added to your total. The dealer can also choose to take another card. But be careful not to get more than 21 points, because if you do, you lose! The player who gets closest to 21, without going over it, wins!
In this adventure we code the first part of our Blackjack game. We'll create a function to calculate how many points a card is worth.
Set the variables Start by making a list of all the cards, from 2 to Ace. Next make a list of the face cards, so Jack, Queen and King. Then pick a random card from the list of cards to be card_1.
Create a function to calculate the points
Create a function that calculates how many points a card is worth.
All the face cards are worth 10 points, the Ace is worth 11 and all the other cards are worth their numeral.
Return the variable points
at the end of the function.
Test the function
Test if your function is working properly. Finish the first print
command by filling in which card you've drawn. Then finish the second line by calling the function with card_1.
Run the code a couple of times. Are you happy with the results? Great! Then you can remove the testing part and move on to the next adventure!
print 'BLACKJACK'
# Set these variables
cards = _
face_cards = _
card_1 =
# Create a function to calculate the points
define calculate_points with card:
if card in face_cards:
points = _
elif _
_
else:
_
_ points
# Test your function
print 'Your card is a ' _
print 'That is worth ' _ ' points'.
In this adventure we code the second part of our Blackjack game.
Paste your code from the previous adventure In the previous adventure you've started a list of variables and created a function to calculate how many points a card is worth. Copy your code and paste it here. Mind that you don't need the testing part, so if you haven't removed that yet, please do so now.
Add more variables
You have already set the lists cards
and face_cards
and the variable card_1
. Underneath those variables create 3 more variables: card_2
, dealer_card_1
and dealer_card_2
. These variables are all set to a random card from the list of cards.
Add up points To calculate how many points you have scored we call the function with card 1 and we do it again for card 2. Then we add both these scores together to get your total. Do the same thing for the dealers points, but be sure to use the dealer's cards and not your own!
2 Aces You're doing great! Almost all scores can be calculated now. There is only one exception: 2 Aces. If you get 2 Aces, your total is 12 points and not 22 (because 22 points would be losing!). This of course also goes for the dealer.
Show the score Lastly, you want to tell the program to tell you which cards you have drawn and how many points that is. Then show which cards the dealer has and how many points they have.
Continue in the next adventure Great! You have finished this part of the game! Copy your code and go to the next adventure to learn how to ask for an extra card and to declare a winner.
# Paste your code from the previous adventure here
# Add these variables to the list of variables
card_2 = _
dealer_card_1 = _
dealer_card_2 = _
# Add up your points
your_points_1 = call _ with card_1
your_points_2 = _
your_total = _
# Add up the dealers points
dealer_points_1 = _
_
_
# 2 Aces
if card_1 == 'Ace' and _
your_total = 12
if dealer_card_1 _
dealer_total = _
# Show the score
print 'You have drawn a ' _ ' and a ' _ '. That is ' _ ' points'
print 'The dealer has drawn a ' _ ' and a ' _ '. That is ' _ ' points'
In the previous adventures you have learned how to draw 2 random cards for yourself and for the dealer and to calculate how many points you both got. In this adventure we add the option to ask for an extra card for both you and the dealer.
Paste your code from the previous adventure Firstly, copy your code from the previous adventure and paste it here.
Extra card for you If you want, you can get an extra card to get your total as close to 21 as possible. First ask the player if they want an extra card. If they do, pick a random card and print what they have drawn. If the card is not an Ace, you can call the function and add the points to your total. In case the card is an Ace, you can't use the function, because the Ace can be either 1 point or 11 points, depending on how many points you already have earned. If your total is less than 11, you want the ace to be 11 points (because this is closest to 21). So you add 11 points to your total. If the total is more than or equal to 11, you want the ace to be 1 point (because you don't want more than 21 points). So you add 1 point to your total. Lastly, print your new total of points.
Extra card for the dealer The dealer can also get an extra card. The dealer doesn't need to be asked, because they always get an extra card if their total is less than 17. Copy the 'Extra card for you code' and paste it in the dealers section. Then change it to fit the dealer picking an extra card and getting points added to their total.
# Paste your code from the previous adventure here
# Extra card for you
hit = ask _
if hit == 'yes':
card_3 = _
print _
if card_3 _ 'Ace':
your_points_3 = _
your_total = _
else:
if your_total _
_
else:
_
print _
# Extra card for the dealer
if dealer_total < 17
_
In the last 3 adventures you have almost created a working blackjack game! The only thing left to do is to decide a winner!
Paste your code from the previous adventure Start by pasting the code that you've made so far into your programming field.
Decide a winner Firstly, if you and the dealer have an equal amount of points, it's a draw. Secondly, if the dealer has more than 21 points and you don't, you are the winner. Thirdly, if both you and the dealer have less than 22 points, we have to see who came closest to 21. We do that by comparing who has the highest score. Is your total higher than the dealer's total, then you are the winner. If not, the dealer wins. Lastly, in all other scenarios (e.g. you have more than 21 points and the dealer doesn't, or you both have more than 21 points) you are the loser.
Enjoy the game! Does your game work properly? Amazing! You have done a great job! Enjoy your game! If it doesn't work right away, no worries, you might have made a mistake. Just keep calm and debug your code using the ladybug button.
# Paste your code from the previous adventure here
# Decide a winner
if _
print 'Its a draw! Play again!'
elif _
print 'You win!'
elif _ :
if _:
print _
else:
print _
else:
_
Debug this code. Good luck!
Warning! This code needs to be debugged!
define food_order
toppings = ask 'pepperoni, tuna, veggie or cheese?'
size = ask 'big, medium or small?'
number_of_pizza = ask 'How many these pizzas would you like?'
print 'YOU ORDERED'
print number_of_pizzas ' size ' topping ' pizza'
define drinks_order
drink = ask 'water, coke, icetea, lemonade or coffee?'
number_of_drinks = ask 'How many of these drinks would you like?'
print 'YOU ORDERED'
print number_of_drinks ' ' drink
'Welcome to Hedy pizza'
more_food = ask 'Would you like to order a pizza?'
while more_food = 'yes'
return food_order
more_food = ask 'Would you like to order a pizza?'
more_drinks = ask 'Would you like to order some drinks?'
while more_drinks == 'yes'
call drink_order
more_drinks == ask 'Would you like to order more drinks?'
print 'Thanks for ordering!'
An error occurred.
An error occurred.
Success
Header
Waiting for a keypress...
Sleeping...
You must be logged in to hand in an assignment.