Python 我怎样才能加上这些咖啡的价格?
例:2份订单。第一个订单是1美元,第二个订单是2美元,所以总价是3美元Python 我怎样才能加上这些咖啡的价格?,python,Python,例:2份订单。第一个订单是1美元,第二个订单是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] capsul
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)
如果你想对循环中的所有值求和,你必须按照@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
使用加法<代码>价格1+价格2。如果您询问如何在循环的迭代中求和,则必须在循环外部将一些变量初始化为
0
,并将价格添加到其中。