如何添加这些咖啡价格?

时间:2018-08-15 13:41:52

标签: python

示例:2个订单。 1s订单是$ 1,2订单是$ 2,所以总价是$ 3。

N = int(input())
for i in range(N):
    price = float(input())
    date = str(input())
    parse = datetime.datetime.strptime(date, "%d/%m/%Y")
    date1 = calendar.monthrange(parse.year, parse.month)[1]
    capsule = int(input())
    cofeeprice = (date1*capsule)*price
    print(cofeeprice)

1 个答案:

答案 0 :(得分:1)

如果要对循环中的所有值求和,则必须按照@Patrick Haugh在注释中所说的那样进行操作,并在循环外添加一个变量。该变量将在每次循环通过时增加,最后将具有所有订单价格的总和。

N = int(input())
sum = 0
for i in range(N):
    price = float(input())
    date = str(input())
    parse = datetime.datetime.strptime(date, "%d/%m/%Y")
    date1 = calendar.monthrange(parse.year, parse.month)[1]
    capsule = int(input())
    cofeeprice = (date1*capsule)*price
    sum = sum + cofeeprice
    print(cofeeprice)
    print(sum) # the price of all orders so far added together

print(sum) # the price of all orders added together