【问题标题】:Python 3.5 Fast Food CalculatorPython 3.5 快餐计算器
【发布时间】:2018-04-11 21:31:14
【问题描述】:

我正在用 Python 编写一个简单的程序。但我不明白为什么它不存储 Hcount、Scount 和 Fcount 的值。此外,它没有正确显示税收。任何帮助将不胜感激

while True:

print("MacDoogle's:")
print("1. Hamburger = $1.50")
print("2. Soda      = $1.15")
print("3. Fries     = $1.25")
print("4. Complete Order")


choice = int(input('Make a selection: '))
while choice < 1 or choice > 4:
    choice = int(input('Enter a valid selection: '))

Hcount =0
Scount =0
Fcount =0 

if choice > 0 and choice <4:

    if choice == 1:
        amount1=int(input("Enter number of Hamburgers: "))
        Hcount += amount1

    elif choice == 2:
        amount2=int(input("Enter number of Sodas: "))
        Scount += amount2

    elif choice == 3:
        amount3=int(input("Enter number of Fries: "))
        Fcount += amount3

if choice == 4:
    sub=int((Hcount * 1.50) + (Scount * 1.15) + (Fcount * 1.25))
    tax=int(sub * 0.09)
    total=int(sub + tax)
    print('Number of Hamburgers:', format(Hcount, '3.0f'))
    print('Number of Sodas:', format(Scount, '3.0f'))
    print('Number of Fries:', format(Fcount, '3.0f'))
    print('Subtotal:', format(sub, '3.2f'))
    print('Tax:', format(tax, '3.2f'))
    print('Total:', format(total, '3.2f'))
    break

【问题讨论】:

  • 修正缩进。这不是有效的 Python 代码。
  • 你的变量也不应该以大写字母开头。 Hcount 应该是 h_counthcount
  • 你还有一堆极其多余的ifs。
  • @AK47。好点,但公平地说,这不太可能是代码失败的原因。
  • 此外,每次运行 while 循环时,您的变量都会重置为 0。考虑将它们移到 while 循环之外

标签: python python-3.x if-statement while-loop


【解决方案1】:

这不是https://codereview.stackexchange.com/ 但是我有点无聊,想帮助你

  1. 正确缩进您的代码 - 每个 ifelifwhile 语句后应该有 4 个缩进空格
  2. 将计数器变量移到 while 循环之外
  3. 将变量名称改为小写,而不是首字母大写
  4. if choice &gt; 0 and choice &lt; 4 可以重写为0 &lt; choice &lt; 4

  5. 您的if choice &gt; 0 and choice &lt; 4 是多余的。你可以删除这个

  6. 你的内部 while 循环是多余的 - 你根本不需要这个
  7. 您可以在 if 语句中重复使用相同的变量名,无需创建 amount0amount1amount2
  8. 在计算小计和税额时,您不应将值转换为 int() - 这会从您的数字中删除小数位,因此永远不会计算您的税额。
  9. 格式化字符串时,如果要将其格式化为 2 位小数,可以使用 'Subtotal: {:0.2f}'.format(sub)

    hcount = 0
    scount = 0
    fcount = 0
    
    while True:
        print("MacDoogle's:")
        print("1. Hamburger = $1.50")
        print("2. Soda      = $1.15")
        print("3. Fries     = $1.25")
        print("4. Complete Order")
    
        choice = int(input('Make a selection: '))
    
        if choice == 1:
            amount = int(input("Enter number of Hamburgers: "))
            hcount += amount
        elif choice == 2:
            amount = int(input("Enter number of Sodas: "))
            scount += amount
        elif choice == 3:
            amount = int(input("Enter number of Fries: "))
            fcount += amount
        elif choice == 4:
            sub = (hcount * 1.50) + (scount * 1.15) + (fcount * 1.25)
            tax = sub * 0.09
            total = sub + tax
    
            print('Number of Hamburgers: {0}'.format(hcount))
            print('Number of Sodas: {0}'.format(scount))
            print('Number of Fries: {0}'.format(fcount))
            print('Subtotal: {:0.2f}'.format(sub))
            print('Tax: {:0.2f}'.format(tax))
            print('Total: {:0.2f}'.format(total))
            break
    

【讨论】:

    【解决方案2】:

    这是因为每次循环迭代时,Hcount,Scount,Fcount 都被赋值为 0。将 Hcount =0 Scount =0 Fcount =0 放在 while 循环前面,它应该可以解决您的问题

    【讨论】:

      【解决方案3】:

      此转换为int 会丢弃小数点右侧的内容。考虑以美分存储所有内容,首先乘以100

          sub_in_cents = (hcount * 150) + (scount * 115) + (fcount * 125)
          tax_in_cents = int(sub * 0.09)
          total_in_cents = sub_in_cents + tax_in_cents
          total_in_dollars = total_in_cents*1./100
      

      【讨论】:

        猜你喜欢
        • 2013-05-03
        • 2016-10-25
        • 2015-03-10
        • 2011-11-15
        • 1970-01-01
        • 2016-05-19
        • 2014-05-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多