我試圖獲取“你想要多少漢堡”的輸入,但是當我運行程序時,我沒有得到這個選項。我在主函數中缺少什么?當我運行程序時也沒有彈出任何錯誤。def main(): endProgram = 'no' while endProgram == 'no': totalFry = 0 totalBurger = 0 totalSoda = 0 endOrder = 'no' while endOrder == 'no': print ('Enter 1 for Yum Yum Burger') print ('Enter 2 for Grease Yum Fries') print ('Enter 3 for Soda Yum') option = input('Enter now -> ') if option == 1: totalBurger = getBurger(totalBurger) elif option == 2: totalFry = getFries(totalFry) elif option == 3: totalSoda = getSoda(totalSoda) endOrder = input ('would you like to end your order? Enter No if you want to order more items: ') total = calcTotal(totalBurger, totalFry, totalSoda) printRecipt(total) endProgram= input ('do you want to end program? (enter no to process new order)') def getBurger(totalBurger): burgerCount = input ('enter number of burgers you want: ') totalBurger = totalBurgers + burgerCount * .99 return totalBurgers def getFry(totalFry): fryCount = input ('Enter How Many Fries You want: ') totalFry = totalFries + fryCount * .79 return totalFries def getSoda(totalSoda): sodaCount = input('enter number of sodas you would want: ') totalSoda = totalSoda + sodaCount * 1.09 return totalSoda def calcTotal(totalBurger, totalFry, totalSoda): subTotal = totalBurger + totalFry + totalSoda tax = subTotal * .06 total = subTotal + tax return total def printRecipt(total): print ('your total is $', total) main()
3 回答

白衣非少年
TA貢獻1155條經驗 獲得超0個贊
而不是:
if option == 1:
嘗試:
if options == '1'
或者你可以做:
option = int(input('Enter now -> ')
輸入返回的字符串不是 int,因此不會觸發 if 語句。

茅侃侃
TA貢獻1842條經驗 獲得超22個贊
您在比較中混合了字符串和 int
例如,在代碼中:
option = input('Enter now -> ')
if option == 1:
totalBurger = getBurger(totalBurger)
input( ) 返回的值始終是一個字符串,因此當您將其與整數 (1) 進行比較時,結果始終為 False
如果要將用戶輸入用作整數,則需要先將其轉換為一個:
option = input('Enter now -> ')
option = int(option)
if option == 1:
totalBurger = getBurger(totalBurger)
您需要對其他輸入()調用進行類似的更改

炎炎設計
TA貢獻1808條經驗 獲得超4個贊
該行將值作為字符串。option = input('Enter now -> ')
當您進行檢查時,您正在將整數與字符串進行比較。這就是為什么沒有一個條件通過并且您無法接受進一步輸入的原因。option==1
嘗試替換為,它應該工作正常。option = input('Enter now -> ')
option = int(input('Enter now -> '))
添加回答
舉報
0/150
提交
取消