Bondenn
Bondenn

Reputation: 1861

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.

Upvotes: 0

Views: 80

Answers (3)

l4mpi
l4mpi

Reputation: 5149

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.

Upvotes: 2

furas
furas

Reputation: 142909

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

Upvotes: 1

rlms
rlms

Reputation: 11060

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.

Upvotes: 0

Related Questions