Mason
Mason

Reputation: 13

else command not running correctly? (Python 3 Beginner)

I'm working on a combo menu for my first Python program. I've gotten everything to work properly up to this point. I want the program to exit if the user's input isn't equal to the accepted answers. I've tried a few different methods to get this working, but it still just runs like I answered "Yes" or "yes". Any help? Thanks a ton.

Here is my code:

def p(): #Prints new line quiokly
    print()

def error(): #Restarts the main
    question = input("You have entered an invalid option. Would you like to try your order again? \n")
    if question in ("Yes","yes","yes"):
        main()
    else:
        exit

def main(): #Main block of code
    cost = 0 #total cost variable
    if cost == 0:
        print("What type of sandwhich would you like? Refer to the cost and type of sandwhich below.")
        p()
        print("Chicken Sandwhich = $5.25")
        p()
        print("Tofu Sandwhich = $5.75")
        p()
        print("Beef Sandwhich = $6.25") #initial questions
        x = input()
        if x == ("Beef") or x == ("beef"):
            cost += 6.25
            print("You have selected Beef. Your total is so far is $6.25.")
        elif x == ("Chicken") or x == ("chicken"):
            cost += 5.25
            print("You have selected Chicken. Your total is so far is $5.25.")
        elif x == ("Tofu") or x == ("tofu"):
            cost += 5.75
            print("You have selected Tofu. Your total is so far is $5.75.")
        if x not in ("beef" , "Beef" , "Tofu" , "tofu" , "chicken" , "Chicken"): #checks for valid resposne
            error()


        print("Would you like to add a drink to your meal?")
        p()
        yzz = input()
        if yzz == ("Yes") or ("yes"):
            p()
            print("Okay, would you like small, medium, or large? Refer to the prices below.")
            print(cost)
            p()
            print("Small - $1.00")
            p()
            print("Medium - $1.75")
            p()
            print("Large - $2.25")
            p()
            yzzz = input()
            if yzzz == ("Small") or yzzz == ("small"):
                cost += 1
                print("You have selected a small drink. Your total is so far is " + "$%.2f" %cost + ".")
            elif yzzz == ("Medium") or yzzz == ("medium"):
                cost += 1.75    
                print("You have selected a medium drink. Your total is so far is " + "$%.2f" %cost + ".")
            elif yzzz == ("Large") or yzzz == ("large"):
                cost += 2.25
                print("You have selected a large drink. Your total is so far is " + "$%.2f" %cost + ".")
            if yzzz not in ("small" , "Small" , "Medium" , "medium" , "large" , "Large"): #checks for valid response
                error()
    elif yzz not in ("yes","Yes"):
        exit




#Main code starts here!
main()

Upvotes: 1

Views: 55

Answers (1)

Raphael
Raphael

Reputation: 1166

The indent for the line elif yzz not in ("yes","Yes"): is wrong, you need to indent it one more time.

Also the expression if yzz == ('Yes') or ('yes') will always evaluate to True because the or expects a boolean to either side and ('yes') evaluates to True.

instead write if yzz in ['Yes', 'yes']

Upvotes: 2

Related Questions