返回餐饮总成本的Python函数,Python

2024-04-28 15:09:13 发布

您现在位置:Python中文网/ 问答频道 /正文

函数取两个值,即消费税前的餐饮成本。 在使用消费税之前,饮料成本需要打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

Tags: the函数答案calculator餐饮total成本print
1条回答
网友
1楼 · 发布于 2024-04-28 15:09:13

首先,meal_cost = 15%对Python没有任何意义。 当必须应用百分比计算时,请考虑改用系数:

meal_factor = 1.15

用这个系数乘以一个数,就可以算出这个数+15%。你也可以喝点酒。在


然后return total_cost = meal_cost, drinks_cost试图返回赋值的结果,但这是错误的。 您要做的是直接返回值的结果

^{pr2}$

或使用中间变量:

total_cost = meal_cost, drinks_cost
return total_cost

最后,请记住,通过执行meal_cost, drinks_cost操作,您只需生成一个元组,而不是一个加法。 你想要的可能是:

total_cost = meal_cost + drinks_cost

最后,您可能应该查看一下Python 2或{a2}的官方Python教程

相关问题 更多 >