这个函数缺少什么?

2024-09-26 18:05:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图得到输入,当它说“你想要多少个汉堡”,但我没有得到这个选项,当我运行程序。我在主要功能中缺少什么?运行程序时也不会出现错误

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()

Tags: tonoyouinputreturndeftotaloption
3条回答

而不是:

if option == 1:

尝试:

if options == '1'

或者你可以:

option = int(input('Enter now -> ')

输入返回的字符串不是int,因此不会触发if语句

option = input('Enter now -> ')将值作为字符串

在检查option==1时,将整数与字符串进行比较。这就是为什么没有一个条件通过,并且您无法接受进一步的输入的原因

尝试替换option = input('Enter now -> ') 使用option = int(input('Enter now -> '))时,它应该可以正常工作

您在比较中混合了字符串和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()调用进行类似的更改

相关问题 更多 >

    热门问题