我想在另一个函数中使用一个函数的返回值,而无需再次运行该函数。 (Python)

时间:2019-10-16 20:03:49

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

例如,现在每次我使用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()

3 个答案:

答案 0 :(得分:4)

看起来您的主要功能只需要包含一个功能调用

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()

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

添加参数,因此您无需继续调用相同的函数。

    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()