【问题标题】:Python asking for input twice when I need it one time当我需要一次输入时,Python 要求输入两次
【发布时间】: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


【解决方案1】:

调用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)

【讨论】:

    【解决方案2】:

    您在内部一遍又一遍地致电get_cost(),这就是您无法找到问题所在的原因。看到你在 display_total_cost 函数中只调用了一次,但你正在调用其他一些函数,如 compute_tipcompute_taxcompute_grand_total,它们都在内部调用 get_cost() 函数,这就是程序问你的原因对于多个输入。 现在,按照其他人的建议,您可以将返回值存储在一个变量中,然后将其作为参数传递给所有其他函数。

    • 您的程序中还有一个微不足道的 main 函数,它的作用与 display_total_cost 函数相同。
    • meal1 = print('Cost:', format(get_cost(), '.2f')) 这不是正确的语法。你不能在赋值运算符之后使用 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()
    

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 2021-12-19
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 2020-10-25
      • 2016-04-15
      • 2019-08-26
      • 2012-07-15
      相关资源
      最近更新 更多