虽然循环不会停止重复

时间:2015-10-05 19:11:31

标签: python while-loop

围绕while循环的代码在大多数情况下工作正常。然而,即使用户输入5应该使其退出,while循环也不会停止重复。这现在包括与我试图调试的函数相关的所有代码,我为任何混淆道歉:

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)

5 个答案:

答案 0 :(得分:2)

您正在测试donutChoice到此行中的字符串

while donutChoice != "5":

但在这一个中有一个int

if donutChoice is 1:

我假设变量donutChoice是一个int,所以你的while循环应该是这样的

while donutChoice != 5

(顺便说一下,我想you should use '==' instead of 'is')。

答案 1 :(得分:0)

我很确定您错过了Python中的主要概念:7"7" 相同。

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

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

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

while != 5继续循环[-inf-4] U [6-inf]。但是,如果您更改为while <5,则仅会为[-inf-4]循环播放。

答案 2 :(得分:0)

您正在混合字符串和整数。将所有内容转换为int。理想情况下,函数应该都返回整数。

此外,您似乎不必使用所有这些类似的变量。相反,请使用列表:

BOOL

答案 3 :(得分:0)

使用此代码,您将接受输入作为字符串,并将其与整数进行比较。

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

更改此项以强制输入为整数:

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

(如果预计这是一个整数,你应该对你的甜甜圈数量做同样的事情)

在控制台中,您可以看到donutChoice永远不会等于5的原因。

>>> "1" == 1
False

此外,您应该将所有if donutChoice is ...更改为if donutChoice == ...。它适用于低整数,因为python实习数在1到256之间以获得更好的性能(因此is将起作用),如果这些数字增长,它将开始表现得很奇怪。建议始终使用==进行整数比较。

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

答案 4 :(得分:0)

此代码对我来说很好。我使用的是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)

顺序()

<强> TUSHAR