【发布时间】:2021-10-08 10:41:39
【问题描述】:
编写一个程序,询问用户餐费,计算餐费 (18%),计算餐费 (8.25%),然后显示餐费,即膳食的小费金额,膳食的税金,以及餐费总额,即费用,小费和税金的总和
这是我的代码:
def get_cost():
meal = float(input('Enter cost of meal: '))
while meal < 0:
print('Not possible.')
meal = float(input('Enter cost of meal: '))
return meal
def compute_tip():
tip = get_cost()*.18
return tip
def compute_tax():
tax = get_cost()*.0825
return tax
def compute_grand_total():
total = get_cost() + compute_tip() + compute_tax()
return total
def display_total_cost():
meal1 = print('Cost:', format(get_cost(), '.2f'))
tip3 = print('Tip:', format(compute_tip(), '.2f'))
tax3 = print('Tax:', format(compute_tax(), '.2f'))
total2 = print('Total:', format(compute_grand_total(), '.2f'))
return meal1, tip3, tax3, total2
def main():
m, t, ta, to = display_total_cost()
print('Cost:' , format(m, '.2f'))
print('Tip:', format(t, '.2f'))
print('Tax:', format(ta, '.2f'))
print('Total:', format(to, '.2f'))
main()
Python Shell 上的输出:
输入餐费:19.95
费用:19.95
输入餐费:19.95
提示:3.59
输入餐费:19.95
税:1.65
输入餐费:19.95
这可能是一个非常简单的解决方法,但是如果它不再要求用餐,我该如何解决呢?我还没有开始。
【问题讨论】:
-
只调用
get_cost()一次而不是多次如何?您可以将结果保存在变量中并将其传递给其他函数。
标签: python python-3.x