我的食物订购系统中的数字加法运算不正确

时间:2019-03-21 18:26:05

标签: python

prices=print("Mushroom Pies:£1.20     Vegtable Pies:£0.80     Spiced Lentil Pies:£1.40")
vegetablepies=0.80
mushroompies=1.20
spicedpies=1.40
total=vegetablepies+mushroompies+spicedpies
vegetablepies=input("How many vegtable pies do you want?")
mushroompies=input("How many mushroom pies do you want?")
spicedpies=input("How many spiced lentil pies do you want?")
print(total)

这是我的代码,我已经对其进行了一段时间的工作,但是这一部分不起作用,我不确定为什么数学不起作用

2 个答案:

答案 0 :(得分:1)

您的代码顺序是向后的。您想先要求输入,然后再计算总数。在所有三个输入之后移动计算总和的行,如下所示:

vegetablepies=input("How many vegtable pies do you want?")
mushroompies=input("How many mushroom pies do you want?")
spicedpies=input("How many spiced lentil pies do you want?")
total=vegetablepies+mushroompies+spicedpies
print(total)

答案 1 :(得分:1)

这看起来像您想要的:

prices = print('Mushroom Pies:£1.20     Vegtable Pies:£0.80     Spiced Lentil Pies:£1.40')

vegetablepies_price = 0.80
mushroompies_price = 1.20
spicedpies_price = 1.40

vegetablepies = input('How many vegetable pies do you want?')
mushroompies = input('How many mushroom pies do you want?')
spicedpies = input('How many spiced lentil pies do you want?')

total_pies = int(vegetablepies) + int(mushroompies) + int(spicedpies)
total_price = int(vegetablepies)*vegetablepies_price + int(mushroompies)*mushroompies_price + int(spicedpies)*spicedpies_price

print('Total Pies:',total_pies)
print('Total Cost: £{:.2f}'.format(total_price))

此代码将商品价格乘以商品数量,然后返回订单的总费用。为了加总用户输入的数字,您需要将它们从字符串转换为数字。我使用了int(),它将强制数字为整数(假设您不能点半个饼)。