Python_A19
Python_A19
The program will use the following code to control the game play:
CODE PURPOSE
Tells the program to get commands from the random
import random
library. This is needed to pick a random color.
item=input('Which color do you pick? ') Allows the player to guess a color.
if item==jackpot:
Tells the player they won coins if their guess is correct.
print('Jackpot! You win 20 coins.')
global coins Puts the value of coins into the North function.
2. Describe what is happening. Refer to Assignment 18 for your text. For example:
#north
def north(): Add the code to
print('''You walk North. the north function.
You see a bright light in the distance.
It is a Golden Castle.''')
Select N to go North. Read the new part of the storyline. For example:
You enter the castle.
There is a troll with three colored chests.
Pick the correct one to win coins.
The game must pick a random color. This is done using a command from the random library.
Add colors and then pick a random one. Use your skills to complete the code:
Apply your skills to test the program. Look at the random color:
There is a troll with three colored chests.
Pick the correct one to win coins.
winning color yellow
What direction would you like to go?
This line will be used to help you test the program. It tells you
the random color that was picked. You will delete it later.
The player needs to guess the correct colored item to win coins. Use your skills to store their
answer as a variable. The variable name must:
• be meaningful
• start with a letter
• have no spaces
• not be a Python keyword
5. Complete the code. Name the variable and put the color choices in the question:
#pick color
colors=('red', 'yellow', 'blue')
jackpot=(random.choice(colors))
print('winning color', jackpot) Refer to Assignment 18
____=input('Which color do you pick? __, __or __? ') for color choices.
Test the program. Type the winning color. The player should win coins:
There is a troll with three colored chests.
Pick the correct one to win coins.
winning color red
Which color do you pick? red, yellow, or blue? red
Jackpot! You win 20 coins.
7. Test the program again. Type the winning color but use ALL CAPS:
Pick the correct one to win coins.
winning color blue
Which color do you pick? red, yellow, or blue? BLUE
The chest is empty.
Pick the correct code to change the input to lowercase. Add it below the player's choice:
chest=input('Which color do you pick? red, yellow, or blue? ')
add the code here…
= .lower()
variable used for player's input variable used for player's input
OR
= .isalpha()
variable used for player's input variable used for player's input
A player can win coins by exploring. In this game, they win coins when they go North. However,
they could also win coins in other parts of the land. The program needs to store the total amount
of coins they have earned.
Calculate the coins. Complete the code to add the amount of coins:
if chest==jackpot:
print('Jackpot! You win 20 coins.')
coins=coins+___
print('Total Coins:', coins)
Test the program many times. Keep picking the correct color. Check the Total Coins:
Jackpot! You win 20 coins.
Total Coins: 40
The game works! You no longer need the code, print('winning color', jackpot).
10. Change what the game says each time the player wins. Complete each task:
Add a cheer variable to the north function. It should include three options:
cheer=('yippee', 'wow', 'yahoo')
Close Python