我试图获取“你想要多少汉堡”的输入,但是当我运行程序时,我没有得到这个选项。我在主函数中缺少什么?当我运行程序时也没有弹出任何错误。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条经验 获得超21个赞
您在比较中混合了字符串和 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
提交
取消