Python 这个函数缺少什么?
我试图得到输入,当它说“你想要多少个汉堡”,但我没有得到这个选项,当我运行程序。我在主要功能中缺少什么?运行程序时也不会出现错误Python 这个函数缺少什么?,python,python-3.8,Python,Python 3.8,我试图得到输入,当它说“你想要多少个汉堡”,但我没有得到这个选项,当我运行程序。我在主要功能中缺少什么?运行程序时也不会出现错误 def main(): endProgram = 'no' while endProgram == 'no': totalFry = 0 totalBurger = 0 totalSoda = 0 endOrder = 'no' while endOrder
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()
而不是:
if option == 1:
尝试:
或者你可以:
option = int(input('Enter now -> ')
输入返回的字符串不是int,因此不会触发if语句。您在比较中混合了字符串和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)
您需要对其他input()调用进行类似的更改行
option=input('Enter now->')
将值作为字符串
当您检查选项==1
时,您将整数与字符串进行比较。这就是为什么没有一个条件通过,并且您无法接受进一步的输入的原因
尝试替换option=input('enternow->')
使用
option=int(输入('enternow->'))
应该可以正常工作。谢谢大家!int解决了这个问题!!如果答案帮助您解决了问题,您是否可以将其标记为已接受?谢谢我该怎么做才能解决这个问题?回溯(最后一次调用):文件“D:\CISP 300\Lab\Lab5-5.pyfile.py”,第53行,主()文件“D:\CISP 300\Lab\Lab5-5.pyfile.py”,第16行,主totalBurger=getBurger(totalBurger)文件“D:\CISP 300\Lab\Lab5-5.pyfile.py”,第31行,getBurger totalBurger=(totalBurger+burgerCount)*.99 TypeError:不支持+:'int'和'str'的操作数类型这与您最初的问题中的问题相同。无论何时从input()中获取内容,它都将是一个字符串,因此在本例中,您尝试将字符串添加到整数中,这就是为什么会出现TypeError异常-加号(+)运算符表示它不知道如何将字符串添加到整数中。因此,正如我在回答中提到的,您需要将input()的返回值转换为代码中任何地方的整数。因此,在本例中:burgerCount=int(输入('enternumber of burgers your want:'))就可以做到这一点
option = input('Enter now -> ')
option = int(option)
if option == 1:
totalBurger = getBurger(totalBurger)