Python A8
Python A8
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.
Go to the place where you saved the monster file. Select it. Click Open.
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
#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
#find match
while find:
horns=input('Do you want a pet with horns? yes or no ')
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
You need to add code that will match a person to a pet if they answer yes.
#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.')
When you see the question you just added, type yes.
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.')
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.
>>>
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
break
else:
Apply your skills to test that the loop ends when there is a match.
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
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.
>>>
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:
11. Create a new variable to turn eyes into an integer. Edit the code:
Apply your skills to test the program again. Pick a different value such as 2 or 3.
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, 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