Python I';我用Pyton创建了商店计算器,但它有一些bug
因为你们将在我的代码中使用,我在商店里有3件物品,想向顾客展示他买了多少 但是代码不起作用!!! 谁能帮我修一下吗?我找不到确切的问题!!! 非常感谢你Python I';我用Pyton创建了商店计算器,但它有一些bug,python,python-3.x,function,Python,Python 3.x,Function,因为你们将在我的代码中使用,我在商店里有3件物品,想向顾客展示他买了多少 但是代码不起作用!!! 谁能帮我修一下吗?我找不到确切的问题!!! 非常感谢你 apple = 0.50 orange = 0.75 banana = 0.25 m = "f" def calculator(): m = input("Which fruit u want to purchase? \n press < a > for apple \n press <
apple = 0.50
orange = 0.75
banana = 0.25
m = "f"
def calculator():
m = input("Which fruit u want to purchase? \n press < a > for apple \n press < o > for orange \n press < b > for banana \n press e for exit: ")
a_p = 0
o_p = 0
b_p = 0
total = a_p + o_p + b_p
if m == "a":
a_t = eval(input("How many ? "))
a_p = apple * a_t
total = a_p + total
main()
if m == "o":
o_t = eval(input("How many ? "))
o_p = orange * o_t
total = total + o_p
main()
if m == "b":
b_t = eval(input("How many ? "))
b_p = banana * b_t
total = total + b_p
main()
else:
total = str(total)
print("You've purchased " + total + " dollar from our shop \n Have a nice day !!!")
def main():
calculator();
main()
apple=0.50
橙色=0.75
香蕉=0.25
m=“f”
def计算器():
m=输入(“你想买哪种水果?\n按苹果\n按橘子\n按香蕉\n按e退出:”)
a_p=0
o_p=0
b_p=0
总计=a_p+o_p+b_p
如果m==“a”:
a\u t=eval(输入(“多少?”)
a_p=苹果*a_t
总计=a_p+总计
main()
如果m==“o”:
o_t=eval(输入(“多少?”)
o_p=橙色*o_t
总计=总计+o\U p
main()
如果m==“b”:
b_t=eval(输入(“多少?”)
b_p=香蕉*b_t
总计=总计+b_p
main()
其他:
总计=str(总计)
打印(“您已经从我们的商店购买了“+total+”美元\n祝您愉快!!!”)
def main():
计算器();
main()
这是因为在输入金额后什么也没有发生。请参见第一个if
块。你调用了main()
,但随后你就没有任何东西可以打印了,因为你打印总数的地方是在else
中
if m == "a":
a_t = eval(input("How many ? "))
a_p = apple * a_t
total = a_p + total
main() # nothing happens after this, it cannot enter the `else` block
else:
total = str(total)
print("You've purchased " + total + " dollar from our shop \n Have a nice day !!!")
您正试图以递归方式执行此操作,尽管这可以奏效,但最好使用while
循环
为清晰起见,示例:
total = 0
product = None
while product != "e":
m = input("Which fruit u want to purchase? \n press < a > for apple \n press < o > for orange \n press < b > for banana \n press e for exit: ")
number = eval(input("How many ? "))
if m == "a":
p = apple * number
elif m == "o":
p = orange * number
total += p
product = m # when this is "e" it will break the while loop
print("You've purchased " + total + " dollar from our shop \n Have a nice day !!!") # will print after the while loop has finished
total=0
产品=无
而产品!=“e”:
m=输入(“你想买哪种水果?\n按苹果\n按橘子\n按香蕉\n按e退出:”)
数字=评估(输入(“多少?”)
如果m==“a”:
p=苹果*数字
elif m==“o”:
p=橙色*数字
总数+=p
product=m#当这是“e”时,它将中断while循环
打印(“您从我们的商店购买了“+total+”美元\n祝您愉快!!!”)#将在while循环完成后打印
非常感谢你,伙计!!!祝你一切顺利