I would like to use the return value of a function in another function, without running through that function again. (Python)

Viewed 1358

Right now every time I use cost = get_cost(), for instance, the function goes through and asks for an input again. Is there a way to have just the return value be what is saved, so I can use that value in another function? Thanks for all the help.

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 Answers

it looks like your main function only needs to include the one function call

def main():
    display_total_cost()

because display_total_cost() internally calls all your other functions as needed.

then you can pass cost as a function parameter to the other functions that require it and remove the get_cost calls from all the other functions. Here is an updated version of your script:

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

You have to save the value once its returned, and pass it to the other functions. Something like this:

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

But you would also have to rewrite the functions to accept these arguments, for example:

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

Add in parameters, so you don't need to keep calling the same functions.

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