0% found this document useful (0 votes)
11 views5 pages

Python A8

This document outlines the steps to complete the Pet Monster Rescue program, focusing on building decision-making into the code through user input and conditional statements. It includes instructions for creating a loop to ask questions about pet preferences, matching users to pets based on their answers, and handling various conditions such as the number of eyes and arms. Additionally, it suggests enhancements to improve the program's functionality and user experience.

Uploaded by

biji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Python A8

This document outlines the steps to complete the Pet Monster Rescue program, focusing on building decision-making into the code through user input and conditional statements. It includes instructions for creating a loop to ask questions about pet preferences, matching users to pets based on their answers, and handling various conditions such as the number of eyes and arms. Additionally, it suggests enhancements to improve the program's functionality and user experience.

Uploaded by

biji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Session 2

Build Decision Making into the Code


In this assignment, you complete the Pet Monster Rescue
program using your plan from Assignment 7. You will write
code that asks questions. The answers will be used to match
a person to their ideal pet.

The program will make decisions using the following code:

CODE PURPOSE
find=True A variable that starts and stops the search for a pet. While find is set to True the
find=False person is asked a list of questions. When find changes to False the search ends.

while find: A loop that contains a list of questions. The person will be asked each question in
order. If no match is found to a pet, the search ends.
if horns=='yes': A statement that finds a pet with horns if the person's answer is 'yes'.
if body!='furry': A statement that finds a pet if the person's answer is not 'furry'.
if num_eyes<2: A statement that finds a pet with one eye if the number is less than 2.
if num_arms>3: A statement that finds a pet with 4 or more arms if the number is greater than 3.
A command that stops the loop. This happens when a person finds a pet and no
break
longer needs to answer questions.
An action that happens if there are no matches after answering all the questions.
else
find changes to False causing the search to end.

Open the Saved Monster File in IDLE

1.  Open IDLE (Python).

 From the File menu, select Open.

 Go to the place where you saved the monster file. Select it. Click Open.

Control the Search

2.  A find variable will be used to start and stop the search. Add it to the variable section:

#variables
place=('Pet Monster Rescue') The find variable will be
used to make a loop.
find=True

 Create a loop that controls a list of questions:

#pet owner
name=input('What is your name? ')
print('Hello', name+'.')
home=input('Is your home calm or busy? ')
print('Okay. Your home is a', home, 'place.')
own=input('How many pet monsters do you own? ')
print('So you have', own+'.')

#find match
while find: press ENTER

Copyright © TechnoKids Inc. 31 TechnoPython | Python


Session 2

Find a Match if the Person Wants a Pet with Horns

3.  Ask a question and store the answer as the variable horns:

#find match
while find:
horns=input('Do you want a pet with horns? yes or no ')

The answer will be used to find a pet with horns.

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

 From the Run menu, select Run Module.

 The question repeats. This is because there is nothing to stop the loop yet:
Do you want a pet with horns? yes or no yes
Do you want a pet with horns? yes or no no
Do you want a pet with horns? yes or no yes
Do you want a pet with horns? yes or no

 Close the Python Shell.

You need to add code that will match a person to a pet if they answer yes.

4.  Add an if statement that matches a person to a pet with horns:

#find match
while find:
horns=input('Do you want a pet with horns? yes or no ')
if horns=='yes':
print('We have a match.')
print('Describe the pet. Refer to Assignment 7.')

What is the pet's name? What does it eat and play?

Test the Code

5.  Apply your skills to run the program.

 When you see the question you just added, type yes.

 Read about the matching pet monster:


Do you want a pet with horns? yes or no yes
We have a match.
It is Furhop. He likes to eat pizza and play ring toss.
Do you want a pet with horns? yes or no

 When you see the question again, type no.

The question keeps repeating itself. You


need to add code that stops the loop.

 Close the Python Shell.

Copyright © TechnoKids Inc. 32 TechnoPython | Python


Session 2

Stop the Loop to End the Search

6.  If there is no match, the search needs to end. Set the find variable to False:
if horns=='yes':
print('We have a match.')
print('It is Furhop. He likes to eat pizza and play ring toss.')

else: press BACKSPACE


find=False
print('We do not have a matching pet.')

 Test the code. Run the program. When you see the question you just added, type no:
Do you want a pet with horns? yes or no no
We do not have a matching pet.
>>>

 Close the Python Shell.

7.  Run the program again. When you see the question, type yes:
Do you want a pet with horns? yes or no yes
We have a match.
It is Furhop. He likes to eat pizza and play ring toss.
Do you want a pet with horns? yes or no

The loop needs to stop if there is a match.


This can be done using break.

 Close the Python Shell.


 Add break to stop the loop:
if horns=='yes':
print('We have a match.')
print('It is Furhop. He likes to eat pizza and play ring toss.')

break
else:

 Apply your skills to test that the loop ends when there is a match.

Make a Match if the Answer is Not Furry

8.  Ask another question and store the answer as the variable body:
if horns=='yes':
print('We have a match.')
print('It is Furhop. He likes to eat pizza and play ring toss.')
press ENTER
break

body=input('What type of body do you like? slimy, scaly, or furry? ')


else:

The answer will be used to find a pet that is not furry.

 Add an if statement that matches a person to a pet that is not furry:


body=input('What type of body do you like? slimy, scaly, or furry? ')
if body!='furry':
print('You answered', body)
print('We have a match. Describe the pet. Refer to Assignment 7.')
break

Copyright © TechnoKids Inc. 33 TechnoPython | Python


Session 2

Be Logical – Test One Value at a Time

9.  Apply your skills to run the program.

 When you are asked if you want a pet with horns, type no.
Do you want a pet with horns? yes or no no

Since there is no match, the program will ask the next question.

 When you are asked about the body of the pet, type furry.
What type of body do you like? slimy, scaly, or furry? furry
We do not have a matching pet.
>>>

The program only finds a match if the


answer is different from furry. Try it!

 Apply your skills to test the program again:


Do you want a pet with horns? yes or no no
What type of body do you like? slimy, scaly, or furry? slimy
You answered slimy
We have a match. It is Slimo. He likes to eat bugs and swim in mud.
>>>

 Close the Python Shell.

Make a Match if the Person Wants Less than 2 Eyes

10.  Ask another question and store the answer as the variable eyes. Add the logic:
eyes=input('How many eyes do you want your pet to have? ')
if eyes<2:
print('You want a pet with', eyes, 'eye.')
print('We have a match. Describe the pet. Refer to Assignment 7.')
break
else:

 Apply your skills to test the program:


Do you want a pet with horns? yes or no no
What type of body do you like? slimy, scaly, or furry? furry
How many eyes do you want your pet to have? 1
Traceback (most recent call last):
File "C:\Users\Student\Documents\monster.py", line 36, in <module>
if eyes<2:
TypeError: '<' not supported between instances of 'str' and 'int'

The answer to a question is stored as a string. Text is not a


number value. You must change the variable eyes to an integer.

 Close the Python Shell.

Copyright © TechnoKids Inc. 34 TechnoPython | Python


Session 2

Change the Variable Type so the Logic Can Work

11.  Create a new variable to turn eyes into an integer. Edit the code:

eyes=input('How many eyes do you want your pet to have? ')


int is integer
num_eyes=int(eyes) for short
if num_eyes<2:
print('You want a pet with', num_eyes, 'eye.')

 Apply your skills to test the program again:


Do you want a pet with horns? yes or no no
What type of body do you like? slimy, scaly, or furry? furry
How many eyes do you want your pet to have? 1
You want a pet with 1 eye.
We have a match. It is Cyclopee. He likes to eat worms and sleep.

 Apply your skills to test the program again. Pick a different value such as 2 or 3.

Make a Match if the Person Wants More than 3 Arms

12.  Ask another question and store the answer as the variable arms. Add the logic:
arms=input('How many arms do you want your pet to have? ')
num_arms=int(arms)
if num_arms>3:
print('You want a pet with', num_arms, 'arms.')
print('We have a match. Describe the pet. Refer to Assignment 7.')
break
else:

 Apply your skills to test the program. Test it with 2 arms. Test it again with 4 arms.
Do you want a pet with horns? yes or no no
What type of body do you like? slimy, scaly, or furry? furry
How many eyes do you want your pet to have? 3
How many arms do you want your pet to have? 2
We do not have a matching pet.

 Close the Python Shell. Do you want to repeat the questions? Refer
to the Session 2 Skill review.
Do you want to show a picture of the pet?
Take the Coding Challenge Refer to the Session 2 Extension Activity.

13. Pick from the options below to make your program even better:

 Add a period to complete the sentence about the body: You answered slimy.

 Add the text Let's keep looking before the body, eyes, and arms questions.

 Add the question, Do you want a pet with wings?


If yes, the person should match to a pet.

 Add the question, How many legs do you want your pet to have?
If the number is greater than 2, the person should match to a pet.

Close Python

Copyright © TechnoKids Inc. 35 TechnoPython | Python

You might also like