【发布时间】: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_count或hcount -
你还有一堆极其多余的
ifs。 -
@AK47。好点,但公平地说,这不太可能是代码失败的原因。
-
此外,每次运行 while 循环时,您的变量都会重置为 0。考虑将它们移到 while 循环之外
标签: python python-3.x if-statement while-loop