Python中的总计问题 [英] Problem in totaling in Python
本文介绍了Python中的总计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用python 2.7.13。我必须制作一个输入产品类型的程序,然后总计所有输入产品的数量。我尝试制作一个,但它不起作用。
共有5种类型的产品可以输入。这是他们的名字和价格
bun 0.50
咖啡1.20
蛋糕1.50
三明治2.10
甜点4.00
我尝试过:
I am using python 2.7.13. I had to make a program that inputs a product type and then totals the amount for all the products entered. I tried making one but it isn't working.
there are total 5 types of products that can be entered. Here are their names and prices
bun 0.50
coffee 1.20
cake 1.50
sandwich 2.10
dessert 4.00
What I have tried:
while True:
item = str(input("Which item would you like to buy?"))
elif item == 'bun':
abun = abun + 0.50
nbun += 1
continue
elif item == 'coffee':
acoffee = acoffee + 1.20
ncoffee += 1
continue
elif item == 'cake':
acake = acake + 1.50
ncake += 1
continue
elif item == 'sandwich':
asandwich = asandwich + 2.10
nsandwich += 1
continue
elif item == 'dessert':
adessert = adessert + 4.00
ndessert += 1
continue
user_input = raw_input('type yes to continue and end to terminate:')
if user_input == 'end':
break
total = abun + acoffee + acake + asandwich + adessert
推荐答案
嗨试试这个
我假设你要把所有变量都归零
Hi try this
I assume you are initailising all variables to zero
abun = 0
acoffee=0
acake=0
asandwich=0
adessert=0
nbun=0
ncake=0
ncoffee=0
nsandwich=0
ndessert=0
while True:
item = raw_input("Which item would you like to buy?")
if item == 'bun':
abun = abun + 0.50
nbun += 1
elif item == 'coffee':
acoffee = acoffee + 1.20
ncoffee += 1
elif item == 'cake':
acake = acake + 1.50
ncake += 1
elif item == 'sandwich':
asandwich = asandwich + 2.10
nsandwich += 1
elif item == 'dessert':
adessert = adessert + 4.00
ndessert += 1
user_input = raw_input('type yes to continue and end to terminate:')
if user_input == 'end':
break
total = abun + acoffee + acake + asandwich + adessert
修复了上面的解决方案总数必须在while循环之外
fixed the solution above total has to be outside the while loop
这篇关于Python中的总计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文