[英]While Loop Won't Stop Repeating
在大多數情況下,我圍繞 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)
您正在測試 donutChoice 到此行中的字符串
while donutChoice != "5":
但是在這個中有一個 int
if donutChoice is 1:
我假設變量 donutChoice 是一個 int 所以你的 while 循環應該是這樣的
while donutChoice != 5
(順便說一句,我認為您應該使用 '==' 而不是 'is' )。
我很確定您在 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]
。
您正在混合字符串和整數。 將所有內容轉換為 int。 理想情況下,函數都應該返回整數。
此外,您似乎不必使用所有這些相似的變量。 相反,使用列表:
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
使用此代碼,您接受作為字符串的輸入,並將其與整數進行比較。
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 == ...
。 它將為低整數的工作,因為有更好的表現1和256之間的蟒蛇實習生號碼(以便is
將工作),它會開始表現古怪,如果這些數字增長。 建議始終使用==
進行整數比較。
>>> a = 1
>>> a is 1
True
>>> a = 257
>>> a is 257
False
此代碼對我來說工作正常。 我正在使用 python 3.3.3
定義順序():
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)
命令()
圖沙爾
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.