简体   繁体   中英

Loop issue with python3

I'm struggeling a bit with a part of code for a little program I'm writing. Have in mind I'm very new with this.

Heres the code:

def sell():
    sell = input("\nGreetings! What would you like to do today?\nPress 1 to sell an animal\nPress 2 to buy an animal\nPress 3 If you want to see all the farms and their animals first\n")

    if sell == "1":
        whichs = input("Which animal do you want to sell?\nPress 1 for pig\nPress 2 for horse\nPress 3 for cow\nPress 4 for bull\n")
        if whichs == "1":
            print ("\nYou just sold\n",p[0])
            print ("\nYou now have 350gold")
            print ("\nThese are the animals you have left:")
            print (p[1], p[2], p[3]) #Prints the animals you have left from p list.
        elif whichs == "2":
            print ("\nYou just sold\n",p[1])
            print ("\nYou now have 350gold")
            print ("\nThese are the animals you have left:")
            print (p[0], p[2], p[3])
        elif whichs == "3":
            print ("\nYou just sold\n",p[2])
            print ("\nYou now have 360gold.")
            print ("\nThese are the animals you have left:")
            print (p[0], p[1], p[3])
        elif whichs == "4":
            print ("\nYou just sold\n",p[3])
            print ("\nYou now have 350gold.")
            print ("\nThese are the animals you have left:")
            print (p[0], p[1], p[2])
        else:
            print ("Error")

I want this to loop so when the user has sold one animal, they start over with the:

sell = input("\nGreetings! What would you like to do today?\nPress 1 to sell an animal\nPress 2 to buy an animal\nPress 3 If you want to see all the farms and their animals first\n")

And I'm struggeling with how this is done.

The other two answers are right in telling you to use a while loop but fail to address a more general problem: the loop shouldn't be inside of the sell function but outside of it, as your text indicates that the user can also buy stuff or look at his stats. You should create individual functions for all of these actions and then create a loop that checks for the action and calls the appropriate functions:

def buy():
    #...

def sell():
    #...

def stats():
    #...

while True:
    choice = input("1: Buy 2:Sell 3:Stats - press any other key to exit")
    if choice == "1": buy()
    elif choice == "2": sell()
    elif choice == "3": stats()
    else: break

The loop could be optimized by using more pythonic approaches like mapping the choices to the functions in a dictionary, but I've written it with a simple if for clarity.

Also, if you don't choose to hold all your state in global variables (which you shouldn't), it would make sense to put the functions into a class which also holds the current balance, stock and other game parameters.

def sell():
    looping = True

    while looping:
        sell = input("\nGreetings! ... Press Q for quit")

        if sell == "1":
            #rest of your code
        elif sell == "Q":
            looping = False

Try using a while loop:

def sell_function():
    while True:
        sell = input("\nGreetings! What would you like to do today?\nPress 1 to sell an animal\nPress 2 to buy an animal\nPress 3 If you want to see all the farms and their animals first\n")
        # ...
            else:
                print("Error")
                break        # Stop looping on error

We could have also set a variable to True , done while variable and then variable = False instead of break for the same effect (in this instance).

I renamed your function, as you used a variable called sell and had a function with the same name which could cause problems. Also, you'll no doubt later find the break and continue statements useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM