In Python 3.2, I’m writing up a basic menu program, and when the option to quit is entered, the function is not ending.
When quit is chosen, it ends the loop the rest of the script is in, and should terminate the script, but it isn’t, for whatever reason?
Am I missing an ‘end’ function that kills the script, or is the new Python Shell just buggy?
Pretty sure this wasn’t necessary in Python 2.7.
import random
choice = str(input("\nMenu:\n(I)nstructions\n(C)alculate blah\n(Q)uit\n>>>"))
while choice != "q" or choice != "Q":
while choice != "i" and choice != "I" and choice != "c" and choice != "C" and choice != "q" and choice != "Q":
print("Invalid menu choice.")
choice = str(input("\nMenu:\n(I)nstructions\n(C)alculate blah\n(Q)uit\n>>>"))
if choice == "i" or choice == "I":
print("blahblah.")
choice = str(input("\nMenu:\n(I)nstructions\n(C)alculate blah\n(Q)uit\n>>>"))
if choice == "c" or choice == "C":
x = int(input("Please enter the number of x: "))
while x < 0:
x = int(input("Please enter the number of x: "))
y = int(input("Please enter the number of y: "))
while y < 0:
y = int(input("Please enter the number of y: "))
z = str(input("blah (B) or (P) z?: "))
while z != "b" and z != "p" and z != "B" and z != "P":
z = str(input("blah (B) or (P) z?: "))
if z == "b" or z == "B":
total = x*10 + y*6 + 0
print("blah $", total, " blah ", x, " x and ", y, " y. blah!")
#function that outputs the cost of premium z
if z == "p" or z == "P":
luck = random.randrange(1, 11, 1)
if luck == 10:
total = x*10 + y*6
print("\nblah$", total, " blah z for ", x, " x and ", y, " y. blah!")
#below is the normal function, for when the customer is not a lucky winner
if luck != 10:
total = x*12.50 + y*7.50
print("blah $", total, " blah ", x, " x and ", y, " y. blah!")
choice = str(input("\nMenu:\n(I)nstructions\n(C)alculate\n(Q)uit\n>>>"))
Your condition is wrong:
always returns
True, creating an infinite loop.Also, you’ve got quite a convoluted bit of logic here. This can be simplified a lot: