简体   繁体   中英

While Loop Won't Stop Repeating

My code surrounding the while loop is, for the most part, working fine. However the while loop won't stop repeating even if the user inputs 5 which should make it exit. This now includes all code relevant to the function I am trying to debug, I apologize for any confusion:

def dinos():
welcomeTheCustomer()
selection = "\0"
while selection != "4":
    selection = requestUserInput()

    if selection is "1":
        order()
    elif selection is "2":
        checkOut()
    elif selection is "3":
        startOver()

print("Have a good day!")


def askUserToChooseADonut():
print("\n -MENU-\n 1.Strawberry Twizzler\n 2.Chocolate-dipped Maple Puff\n    3.Vanilla Chai Strudel\n 4.Honey-Drizzled Lemon Dutchie\n 5.Done\n")
donutChoice = int(raw_input("Please enter a selection: "))
return(donutChoice)

def askUserToSpecifyDonutQuantity():
donutQuantity = "d"
while not donutQuantity.isdigit():
    donutQuantity = str(raw_input("Please enter a quantity of donuts: "))
return int(donutQuantity)
print ("Confirmed")

def order():
donutChoice = 0

donutQuantity1 = 0
donutQuantity2 = 0
donutQuantity3 = 0
donutQuantity4 = 0

while not (donutChoice == 5): # loop until the customer selects '5. Done'
    donutChoice = int(askUserToChooseADonut())
    donutQuantity = askUserToSpecifyDonutQuantity()

    if donutChoice is 1:
        donutQuantity1 = donutQuantity
    elif donutChoice is 2:
        donutQuantity2 = donutQuantity
    elif donutChoice is 3:
        donutQuantity3 = donutQuantity
    elif donutChoice is 4:
        donutQuantity4 = donutQuantity

return (donutQuantity1, donutQuantity2, donutQuantity3, donutQuantity4)

you are testing donutChoice to a string in this line

while donutChoice != "5":

but with an int in this one

if donutChoice is 1:

I assume the variable donutChoice is an int so your while loop should be like this

while donutChoice != 5

(Btw, I think you should use '==' instead of 'is' ).

I'm pretty sure you're missing a main concept in Python: 7 and "7" are NOT the same.

>>> a = 7
>>> b= "7"

>>> print(type(a))
<class 'int'>

>>> print(type(b))
<class 'str'>

Having while != 5 will continue looping for [-inf-4] U [6-inf] . But if you change to while <5 will loop only for [-inf-4] .

You are mixing strings and integers. Convert everything to int. Ideally, the functions should all return integers.

Also, it appears that you do not have to use all those similar variables. Instead, use a list:

donutChoice = 0
donutQuantity = [0] * N_DONUT

while donutChoice in range(5):
    donutChoice = int(askUserToChooseADonut())
    quantity = int(askUserToSpecifyDonutQuantity())
    # There should be verification in "askUserToChooseADonut",
    # so this if should not be necessary.
    if donutChoice != 0
        donutQuantity[donutChoice-1] += quantity
return donutQuantity

With this code, you're accepting input as a string, and comparing it to an integer.

while donutChoice != 5: # loop until the customer selects '5. Done'
    donutChoice = askUserToChooseADonut()

Change this to force the input to be an integer:

while donutChoice != 5: # loop until the customer selects '5. Done'
    donutChoice = int(askUserToChooseADonut())

(You should do the same for your donut quantity if this is expected to be an integer)

From the console, you can see why donutChoice will never equal 5.

>>> "1" == 1
False

Furthermore, you should change all your if donutChoice is ... to if donutChoice == ... . It will work for low integers, because python interns numbers between 1 and 256 for better performance (so is will work), it will start to behave weirdly if these numbers grow. It's recommended to always use == for integer comparisons.

>>> a = 1
>>> a is 1
True
>>> a = 257
>>> a is 257
False

This Code is working fine for me. I'm using python 3.3.3

def order():

donutChoice = "0"

donutQuantity1 = 0
donutQuantity2 = 0
donutQuantity3 = 0
donutQuantity4 = 0

while donutChoice != "5": # loop until the customer selects '5. Done'
    donutChoice = input("Enter a choice: ")
    donutQuantity = input("Enter the Quantity: ")

    if donutChoice is 1:
        donutQuantity1 = donutQuantity
    elif donutChoice is 2:
        donutQuantity2 = donutQuantity
    elif donutChoice is 3:
        donutQuantity3 = donutQuantity
    elif donutChoice is 4:
        donutQuantity4 = donutQuantity

return (donutQuantity1, donutQuantity2, donutQuantity3, donutQuantity4)

order()

TUSHAR

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