编写一个程序,向用户询问一顿饭的费用,计算这顿饭的小费(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()
,因此无法找到问题所在。在display\u total\u cost
函数中只调用它一次,但是您正在调用一些其他函数,如compute\u tip
、compute\u tax
和compute\u grand\u total
,其中所有函数都在内部调用get\u cost()
函数,这就是程序要求您输入多个数据的原因。现在,按照大家的建议,您可以将返回值存储在变量中,然后将其作为参数传递给所有其他函数。
main
函数,它与display\u total\u cost
函数的作用相同
meal1=print('Cost:',format(get_Cost(),'.2f'))
这不是正确的语法。您不能在赋值运算符之后使用print语句,即=
,即使它不会引发任何错误,但当您可以只执行print语句时,为什么还要编写额外的内容呢?因为使用带有赋值运算符的print语句不会为变量赋值
compute_grand_total
您不需要反复调用函数来计算以前计算的值,除非您有内存限制并且无法存储值
按以下方法修复:
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(meal):
tip = meal*.18
return tip
def compute_tax(meal):
tax = meal*.0825
return tax
def display_total_cost():
meal = get_cost()
print('Cost:', format(get_cost(), '.2f'))
tip = compute_tip(meal)
print('Tip:', format(tip, '.2f'))
tax = compute_tax(meal)
print('Tax:', format(tax, '.2f'))
print('Total:', format(meal + tip + tax, '.2f'))
display_total_cost()
调用get_cost()
一次并将其分配给变量;然后将其作为参数传递给其他函数。
def compute_tip(meal):
return meal * .18
def compute_tax(meal):
return meal * .0825
def display_total_cost():
meal = get_cost()
return meal, compute_tip(meal), compute_tax(meal), compute_grand_total(meal)
我遇到了一个循环问题,我必须输入两次温度才能启动循环。我想我知道问题在哪里,只是不知道如何解决。我已经编写了总共三周的代码,所以我在这方面完全是个初学者。 这是我有问题的代码部分: 我想你明白我的意思了。我唯一能让循环工作的方法就是重复int.Parse(Console.ReadLine()),但也许还有另一种修复方法可以修复必须输入两次温度的问题? 真希望有人能帮我解决这个问题。
这是菜单类 import java.util.Scanner; 公共类菜单{private String[]Menu_options; } 这是我使用的菜单驱动程序 公共类 Menutester { } 我第一次输入一些东西时,它根本不会重新生成,当我尝试在eclipse中调试它时,它会给我错误FileNotFoundException(Throwable)。(字符串)行:第一次输入195。 这就
我用java编写了一个带条件的switch case语句。如果一个人在2到12个月的时间内将一个项目添加到系统中,它将被添加到一个数组中。如果项目持续时间小于2或大于12,系统应要求他们重新输入有效数字。出于某种原因,我的系统会提示他们输入两次数字,并且只存储数组中的第二个条目:终端提示两次的图像 我在想这是因为我有“myMonths[index]=sc.nextInt();”声明了两次,但是我不
在我的部分代码中,循环中有一个语句,它应该在每次迭代时都请求输入。然而,它只要求输入一次,当循环再次运行时不会再次要求输入,就好像已经输入了一些内容一样。这真的很烦人,因为似乎没有什么问题。
我试图做简单的超文本传输协议后请求使用以下代码:Golang代码从另一个SO后 它发送了两次http请求(我尝试了向自己的web服务和Firebase消息服务器发送)。有人知道怎么回事吗?非常感谢。 编辑忽略我,找出是AVG反病毒造成的问题。如果我这样做:然后按enter键一次,AVG会中断,说它已经扫描了它,然后让它运行。这会导致http调用两次。如果我在运行前禁用了防病毒软件,那么http请求
如何获取小数点后仅两位数的双倍值。如果我的身高输入为72,体重为170,则收到的输出给出BMI=23.053626543209877。我不知道如何去掉.05后的尾随数字