I'm confused about my code, I think I did everything right but I'm not sure how to get the second part of my code to work, the "total_costs" function. I've watched a lot of videos now but can't seem to find one about this, could someone explain to me why I can't get the second part of my code to work?
def get_input():
e1 = float(input("What is the monthly expense for House Payment. "))
e2 = float(input("What is the monthly expense for Homeowners Insurance. "))
e3 = float(input("What is the monthly expense for Car Payments. "))
e4 = float(input("What is the monthly expense for Car Insurance. "))
e5 = float(input("What is the monthly expense for Utilities. "))
print("The monthly expenses total to",format(monthly, ',.2f'))
print("The yearly expenses total to",format(yearly, ',.2f'))
def total_costs():
monthly = e1 e2 e3 e4 e5
yearly = monthly * 12
return monthly, yearly
get_input()
CodePudding user response:
There is nowhere that total_costs is called.
CodePudding user response:
You are using e1,e2,e3,e4,e5 as function variables so the scope is limited to get_input
function only, you can not use those variables outside get_input
function until you specify them globaly.
Moreover you are not calling total_cost
function.
The problem is you are using variables in local scope and not invoking the function.
CodePudding user response:
There are a few issues with your code. First, you're not calling total_costs()
, so it never gets executed. You're also not capturing the result.
Let's rearrange things:
def get_input():
e1 = float(input("What is the monthly expense for House Payment. "))
e2 = float(input("What is the monthly expense for Homeowners Insurance. "))
e3 = float(input("What is the monthly expense for Car Payments. "))
e4 = float(input("What is the monthly expense for Car Insurance. "))
e5 = float(input("What is the monthly expense for Utilities. "))
monthly, yearly = total_costs()
print("The monthly expenses total to",format(monthly, ',.2f'))
print("The yearly expenses total to",format(yearly, ',.2f'))
def total_costs():
monthly = e1 e2 e3 e4 e5
yearly = monthly * 12
return monthly, yearly
get_input()
Better, but when you try, it will complain about not knowing about e1. That's because although it might seem that way, total_costs doesn't know about e1
and other variables. You need to explicitly pass it.
#!/usr/bin/env python3
def get_input():
e1 = float(input("What is the monthly expense for House Payment. "))
e2 = float(input("What is the monthly expense for Homeowners Insurance. "))
e3 = float(input("What is the monthly expense for Car Payments. "))
e4 = float(input("What is the monthly expense for Car Insurance. "))
e5 = float(input("What is the monthly expense for Utilities. "))
monthly, yearly = total_costs(e1, e2, e3, e4, e5)
print("The monthly expenses total to",format(monthly, ',.2f'))
print("The yearly expenses total to",format(yearly, ',.2f'))
def total_costs(e1, e2, e3, e4, e5):
monthly = e1 e2 e3 e4 e5
yearly = monthly * 12
return monthly, yearly
get_input()
Now it all works like you would expect it to work. Now you wouldn't necessarily structure your code like this. For instance it doesn't feel like what you do in get_input
would require its own function, and maybe instead having all these single variables like e1
, you could have a structure like a list or a dictionary that you would pass to total_costs
, but you'll learn that soon enough.