【问题标题】:I would like to use the return value of a function in another function, without running through that function again. (Python)我想在另一个函数中使用一个函数的返回值,而无需再次运行该函数。 (Python)
【发布时间】:2020-02-13 16:25:46
【问题描述】:

例如,现在每次我使用 cost = get_cost() 时,该函数都会通过并再次要求输入。有没有办法只保存返回值,所以我可以在另一个函数中使用该值?感谢大家的帮助。

def get_cost():

    cost = float(input('Please Enter the Cost of the Meal: '))

    while cost <= 0:
        print('The Cost Must Be Greater Than 0!')
        cost = float(input('Please Enter the Cost of the Meal: '))
    else:
        return cost

def compute_tip():

    cost = get_cost()

    finalTip = cost * 0.18
    return finalTip

def compute_tax():

    cost = get_cost()

    finalTax = cost * 0.0825
    return finalTax

def compute_grand_total():

    cost = get_cost()    
    finalTip = compute_tip()
    finalTax = compute_tax()

    grandTotal = cost + finalTip + finalTax
    return grandTotal

def display_total_cost():

    cost = get_cost()
    finalTip = compute_tip()
    finalTax = compute_tax()
    grandTotal = compute_grand_total()

    print('Cost\t$', format(cost, '.2f'))
    print('Tip\t$', format(finalTip, '.2f'))
    print('Tax\t$', format(finalTax, '.2f'))
    print('Total\t$', format(grandTotal, '.2f'))


def main():

    get_cost()

    compute_tip()

    compute_tax()

    compute_grand_total()

    display_total_cost()

main()

【问题讨论】:

  • 您可能需要考虑将cost 传递给需要它的函数,这样您就不需要一次又一次地调用get_cost() 函数。

标签: python python-3.x function return return-value


【解决方案1】:

看起来你的主函数只需要包含一个函数调用

def main():
    display_total_cost()

因为display_total_cost() 会根据需要在内部调用所有其他函数。

然后您可以将cost 作为函数参数传递给需要它的其他函数,并从所有其他函数中删除get_cost 调用。这是您的脚本的更新版本:

def get_cost():

    cost = float(input('Please Enter the Cost of the Meal: '))

    while cost <= 0:
        print('The Cost Must Be Greater Than 0!')
        cost = float(input('Please Enter the Cost of the Meal: '))
    else:
        return cost

def compute_tip(cost):
    finalTip = cost * 0.18
    return finalTip

def compute_tax(cost):
    finalTax = cost * 0.0825
    return finalTax

def display_total_cost():

    cost = get_cost()
    finalTip = compute_tip(cost)
    finalTax = compute_tax(cost)
    grandTotal = cost + finalTip + finalTax

    print('Cost\t$', format(cost, '.2f'))
    print('Tip\t$', format(finalTip, '.2f'))
    print('Tax\t$', format(finalTax, '.2f'))
    print('Total\t$', format(grandTotal, '.2f'))


def main():
    display_total_cost()

# common practice is to include this line
if __name__ == "__main__":
    main()

【讨论】:

  • 谢谢,您的解释简洁透彻
【解决方案2】:

一旦返回值,您必须保存该值,并将其传递给其他函数。像这样的:

def main():
    cost = get_cost()
    tip = compute_tip(cost)
    tax = compute_tax(cost)
    total = compute_grand_total(tip, tax)
    display_total_cost(total)

但您还必须重写函数以接受这些参数,例如:

def compute_tip(cost):
    finalTip = cost * 0.18
    return finalTip

【讨论】:

    【解决方案3】:

    添加参数,这样你就不需要一直调用相同的函数了。

        def get_cost():
    
        cost = float(input('Please Enter the Cost of the Meal: '))
    
        while cost <= 0:
            print('The Cost Must Be Greater Than 0!')
            cost = float(input('Please Enter the Cost of the Meal: '))
        else:
            return cost
    
    def compute_tip(cost):
    
        finalTip = cost * 0.18
        return finalTip
    
    def compute_tax(cost):
    
        finalTax = cost * 0.0825
        return finalTax
    
    def compute_grand_total(cost, finalTip, finalTax):
    
        grandTotal = cost + finalTip + finalTax
        return grandTotal
    
    def display_total_cost(cost):
    
        finalTip = compute_tip(cost)
        finalTax = compute_tax(cost)
        grandTotal = compute_grand_total(cost, finalTip, finalTax)
    
        print('Cost\t$', format(cost, '.2f'))
        print('Tip\t$', format(finalTip, '.2f'))
        print('Tax\t$', format(finalTax, '.2f'))
        print('Total\t$', format(grandTotal, '.2f'))
    
    def main():
    
        cost = get_cost()
    
        display_total_cost(cost)
    
    main()
    

    【讨论】:

      猜你喜欢
      • 2020-09-29
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 2016-10-27
      • 2010-09-15
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      相关资源
      最近更新 更多