Answer to Question #107444 in Python for Prabhjot

Question #107444
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.
1
Expert's answer
2020-04-02T10:11:39-0400

#!/usr/bin/env python3


def calculate_pets_number(pets_dict):

  pets_quantity = 0

  for pet in pets_dict.keys():

    pets_quantity = pets_quantity + pets[pet]

  return pets_quantity

    

pets = {}

while True:

  print("What kind of pet do you have?")

  pet = input()

  if pet == "rock":

    break

  if pet not in pets.keys():

    pets[pet] = 1

    print("You have a " + pet + " with a total of 1 " + pet + ", with a total of " + str(calculate_pets_number(pets)) + "pet(s).")

  else:

    pets[pet] = pets[pet] + 1

    print("You have a " + pet + " with a total of " + str(pets[pet]) + " " + pet + "s, with a total of " + str(calculate_pets_number(pets)) + " pet(s).")


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS