-1

I just started python 2 weeks ago and I don't know how to make my code repeat itself when the input is something like "fox" when that is not one of the three options I'm accepting (horse, oxen, mule). Also if I want to total up the cost of 2 horse and say 3 mules by having it ask "do you want to buy any more animals", how would i do that? Any help would be very appreciated.

zoo = {}

def main():
    print ("Animals available to purchase: " + "horse: 50, oxen: 35, mule: 20")
    total_money = 1000
    print ("You have " + str(total_money) + " to spend")
    cost(total_money)

def animal_cost(total_money):
        animal_type = raw_input("Please enter an animal:")
        print ("Animal Type Entered: " + str(animal_type))
        global animal_quantity
        animal_quantity = int(raw_input("Please enter quantity of animals:"))
        print ("Animal Quantity Entered: " + str(animal_quantity))
        if animal_type in zoo:
            zoo[animal_type] += animal_quantity
        else: zoo[animal_type] = animal_quantity

        while True:
            if animal_type == 'horse':
                return 50 * animal_quantity
            if animal_type == 'oxen':
                return 35 * animal_quantity
            if animal_type == 'mule':
                return 20 * animal_quantity
            else:
                cost(total_money)

def cost(total_money):
    costing = animal_cost(total_money)
    total_money -= costing
    if total_money <= 0:
        print ("No money left, resetting money to 1000.")
        total_money = 1000
        zoo.clear()

    print ("Cost of Animals: " + str(costing))
    print ("Remaining Balance:" + str(total_money))
    choice = raw_input("Do you want to buy any more animals?(Y/N):")
    if choice in('Y','y'):
        cost(total_money)
    elif choice in ('N','n'):
        choice_2 = raw_input("Enter 'zoo' to see the animals you have purchased:")
        if choice_2 in('zoo','Zoo'):
            print zoo
        choice_3 = raw_input("is everything correct?(Y/N):")
        if choice_3 in ('Y','y'):
            print ("Thank you for shopping!")
        elif choice in ('N','n'):
            print ("Restarting Transaction")
            zoo.clear()
            cost(total_money)


if __name__ == '__main__':
    main()
faustas
  • 1
  • 1
  • 2
    try using while loop or put your code in function – Shubham Namdeo Jun 12 '17 at 03:15
  • 2
    try using functions and loops, it makes everything easier and more readable. Define a list of animals and check if the input entered is inside that list, if its not print the input message again using a while True loop – Gabriel Belini Jun 12 '17 at 03:30
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – SiHa Jun 12 '17 at 07:30
  • Check my solution and do mark as correct if it did the job for you. – Shubham Namdeo Jun 13 '17 at 00:12
  • Do not update or change question totally, as answers are given only on the basis of question. If you got what you've want you can add that as an answer. – Shubham Namdeo Jun 14 '17 at 02:18

1 Answers1

1

You may try this enhanced version of your code:

zoo = {}  # creating global dictionary for adding all animals into it.
# function to get animal cost
def animal_cost(total_money):
    animal_type = raw_input("Please enter an animal:")
    print ("Animal Type Entered: " + str(animal_type))
    animal_quantity = int(raw_input("Please enter quantity of animals:"))
    print ("Animal Quantity Entered: " + str(animal_quantity))
    if animal_type in zoo: 
        zoo[animal_type] += animal_quantity
    else: zoo[animal_type] = animal_quantity
    if animal_type == 'horse':
        return 50 * animal_quantity
    if animal_type == 'oxen':
        return 35 * animal_quantity
    if animal_type == 'mule':
        return 20 * animal_quantity

# getting total_money after animal purchase
def cost(total_money):
    costing = animal_cost(total_money)
    total_money = total_money - costing
    if total_money <=0: # condition when money is less than or equal to 0.
        print("No Money left, resetting money to 1000.")
        total_money = 1000

    print ("Cost of Animals:" + str(costing))
    print ("Total Money Left:" + str(total_money))

    # Recursion for buying more animals:
    choice = raw_input("Do you want to buy any more animals?:")

    if choice in ('Yes','y','Y','yes','YES'):
        cost(total_money)

    # you can use this commented elif condition if you want.
    else: # elif choice in('no','No','n','N','NO'):
        print("thankyou!!")
        print("You have total animals: "+str(zoo))

# main function to initiate program
def main():
    print ("Animals available to purchase: " + "horse, oxen, mule")
    total_money = 1000
    print ("You have " + str(total_money) + " to spend")
    cost(total_money)

if __name__ == '__main__':
    main()

This might help you out. Have a look into this for last two lines

Shubham Namdeo
  • 1,845
  • 2
  • 24
  • 40