235k views
5 votes
4.2 Code Practice: Question 2

Instructions
Write a loop that continually asks the user what pets the user has until the user enters rock, in which case the loop ends. It should acknowledge the user in the following format. For the first pet, it should say You have a dog with a total of 1 pet(s) if they enter dog, and so on.

Sample Run
What pet do you have? lemur
What pet do you have? parrot
What pet do you have? cat
What pet do you have? rock
------------
Sample Output
You have a lemur with a total of 1 pet(s)
You have a parrot with a total of 2 pet(s)
You have a cat with a total of 3 pet(s)

User An Phan
by
5.2k points

2 Answers

6 votes

Answer:

pet = input(str("What pet do you have?"))

totalPets = 1

while(pet!="rock"):

print("You have a " + str(pet) + " with a total of " + str(int(totalPets)) + " pet(s)")

totalPets = totalPets+ 1

pet = input(str("What pet do you have?"))

User Armulator
by
5.7k points
1 vote

In python 3:

total = 0

while True:

pet = input("What pet do you have? ")

if pet == "rock":

break

total += 1

print("You have a {} with a total of {} pet(s)".format(pet, total))

I hope this helps!

User Jason Desrosiers
by
5.1k points