0% found this document useful (0 votes)
0 views

Python_A19

Uploaded by

biji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python_A19

Uploaded by

biji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Session 4

Head North to Win Coins


You are on Step 5 of the Adventure Quest mission. You will design a game that has the player
pick a color to win coins. You will add the code to the north function. The game will:
• tell the player what is happening in the quest
• pick a random color
• ask the player to pick a color
• tell the player if they guessed correctly
• calculate the number of coins the player has won

Refer to your plan from Assignment 18.


You will need it to build the code.

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.

colors=('red', 'yellow', 'blue') Lists color choices.

jackpot=(random.choice(colors)) Picks a random color from the choices.

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.')

else: Tells the player their guess is wrong.

coins=0 Stores how many coins the player has won.

global coins Puts the value of coins into the North function.

coins=coins+20 Adds more coins to the existing amount.

print('Coins earned: ', coins) Displays the current value of coins.

Open the Saved Quest


1.  Open IDLE (Python).
 From the File menu, select Open.
 Select quest.

Copyright © TechnoKids Inc. 85 TechnoPython | Python


Session 4

Tell Players What is Happening in the Quest

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.''')

print ('''You enter the castle.


indent
There is a troll with three colored chests.
4
spaces Pick the correct one to win coins.''')

3.  From the File menu, select Save or press CTRL + S.

 From the Run menu, select Run Module.

 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.

 Close the Python Shell.

Pick a Random Color

The game must pick a random color. This is done using a command from the random library.

4.  At the start of the program, import the random library:

#set up the quest


import random

 Add colors and then pick a random one. Use your skills to complete the code:

print('''You enter the castle.


There is a troll with three colored chests. Press ENTER.
Pick the correct one to win coins.''')
Refer to Assignment 18 for
#pick color color choices.
colors=('___', '___', '___')
jackpot=(random.choice(colors))
print('winning color', jackpot)

 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.

Copyright © TechnoKids Inc. 86 TechnoPython | Python


Session 4

Ask the Player to Make a Choice

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.

Tell the Player if They Won Coins

6.  Complete the if and else statements:


if ____==jackpot:
print('Jackpot! You win __ coins.') To win, the player's
choice must match the
else:
random color picked.
____

 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.

 Test the program again. Pick a WRONG color. What happens?

Test Data Entry to Find a Bug

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

Copyright © TechnoKids Inc. 87 TechnoPython | Python


Session 4

Keep Track of Coins

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.

8.  Create a variable to store coins:


#set up the quest
coins=0
import random
play=True

 Put the variable into the function:

def north(): The word global tells the program that


global coins the variable is outside the function.

 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

Delete the Test Line

The game works! You no longer need the code, print('winning color', jackpot).

9.  Delete the line for testing:


jackpot=(random.choice(colors))
print('winning color', jackpot)

Take the Random Challenge

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')

 Have the game pick a random cheer:


say=(random.choice(cheer))

 Say the cheer when the player wins coins:


print(say)

Close Python

Copyright © TechnoKids Inc. 88 TechnoPython | Python

You might also like