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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With