Python函数返回餐饮总费用,python错误

时间:2016-09-06 08:04:29

标签: python function percentage

该功能有两个值,即消费税前的膳食和饮料成本。 在使用GST之前,需要对饮料成本进行30%的折扣。 商品和服务税(GST)需要添加到餐饮和饮料成本中,GST设置为15%,这是我所拥有的,似乎我得到的答案都是相同的,而不是单独的答案,即11.5和17.02。

def dinner_calculator(meal_cost, drinks_cost):
    """ Returns the total cost of the meal """
    meal_cost = 1.15
    drinks_cost = 1.30
    return total_cost = meal_cost, drinks_cost


total_cost = dinner_calculator(10, 0)
print(round(total_cost, 2))     

11.5

total_cost = dinner_calculator(12, 4)
print(round(total_cost, 2)) 

17.02

1 个答案:

答案 0 :(得分:2)

首先,meal_cost = 15%对Python来说并不意味着什么。 当您必须应用百分比计算时,请考虑使用因子:

meal_factor = 1.15

将数字乘以此因子实际上会计算出此数字+ 15%。你可以用饮料做同样的事。

然后return total_cost = meal_cost, drinks_cost尝试返回分配的结果,这是错误的。 你想要做的是直接返回值的结果

return meal_cost, drinks_cost

或使用中间变量:

total_cost = meal_cost, drinks_cost
return total_cost

最后,请记住,通过执行meal_cost, drinks_cost,您只需制作一个元组,但不能添加。 你想要的可能就是:

total_cost = meal_cost + drinks_cost

作为结论,您可能应该查看Python 2Python 3

的官方Python教程。