'unable to get the output of this code. Any help will be appriciated
def expenses():
# Getting the cost of expenses from user "loanPayment, insurance, gas, oil, tires, maintainance."
loanPayment = float(input('Enter monthly payment: $'))
insurance = float(input('Enter monthly insurance: $'))
gas = float(input('Enter monthly gas expanse: $'))
oil = float(input('Enter monthly oil change expense: $'))
tires = float(input('Enter monthly expense on tires: $'))
maintenance = float(input('Enter monthly cost on other maintenances: $'))
def monthlyCost(loanPayment, insurance, gas, oil, tires, maintenance):
# Calculating and printing monthly expenses of automobile\
totalMonthlyCost = loanPayment + insurance + gas + oil + tires + maintenance
print('Average monthly expense of automobile is', format(monthlyCost, '.2f'), sep='')
yearlyCost(totalMonthlyCost)
def yearlyCost(monthly):
yearlyExpense = monthly * 12
print('Average yearly expense of automobile is $', format(yearlyExpense, '.2f'), sep='')
yearlyCost()
expenses()
Solution 1:[1]
There are three problems with your code:
First of all you need to call monthlyCost() at the end of expenses() using the new variables you created.
Second of all there is a typo in monthlyCost() where you use monthlyCost instead of totalMonthlyCost
Finally, you are calling yearlyCost() with no parameter, which you can simply remove beacause it will be called from monthlyCost()
I also added a ' $' to the end of the string in monthlyCost()
Try this:
def expenses():
# Getting the cost of expenses from user "loanPayment, insurance, gas, oil, tires, maintainance."
loanPayment = float(input('Enter monthly payment: $'))
insurance = float(input('Enter monthly insurance: $'))
gas = float(input('Enter monthly gas expanse: $'))
oil = float(input('Enter monthly oil change expense: $'))
tires = float(input('Enter monthly expense on tires: $'))
maintenance = float(input('Enter monthly cost on other maintenances: $'))
monthlyCost(loanPayment, insurance, gas, oil, tires, maintenance)
def monthlyCost(loanPayment, insurance, gas, oil, tires, maintenance):
# Calculating and printing monthly expenses of automobile\
totalMonthlyCost = loanPayment + insurance + gas + oil + tires + maintenance
print('Average monthly expense of automobile is $', format(totalMonthlyCost, '.2f'), sep='')
yearlyCost(totalMonthlyCost)
def yearlyCost(monthly):
yearlyExpense = monthly * 12
print('Average yearly expense of automobile is $', format(yearlyExpense, '.2f'), sep='')
expenses()
Solution 2:[2]
Problems
Local variables. When you define variables inside a function, you'd expect to be using them later within the function. Otherwise, the values are not callable later on in your program unless you
returnthem. Which leads to problem 2...Nothing is returned from your functions. You can't call data from these later as a result.
Solution
This should work.
def expenses():
# Getting the cost of expenses from user "loanPayment, insurance, gas, oil, tires, maintainance."
loanPayment = float(input('Enter monthly payment: $'))
insurance = float(input('Enter monthly insurance: $'))
gas = float(input('Enter monthly gas expanse: $'))
oil = float(input('Enter monthly oil change expense: $'))
tires = float(input('Enter monthly expense on tires: $'))
maintenance = float(input('Enter monthly cost on other maintenances: $'))
# Return a sum
return loanPayment + insurance + gas + oil + tires + maintenance
def monthlyCost():
# Calculating and printing monthly expenses of automobile\
totalMonthlyCost = expenses()
print(f'Average monthly expense of automobile is {totalMonthlyCost}.')
# Return the statement as well in case you are printing it.
return f'Average monthly expense of automobile is {totalMonthlyCost}.'
def yearlyCost():
yearlyExpense = expenses() * 12
print(f'Average yearly expense of automobile is ${yearlyExpense}.')
# Return the statement as well in case you are printing it.
return f'Average yearly expense of automobile is ${yearlyExpense}.'
if __name__ == "__main__":
yearlyCost()
Result
Enter monthly payment: $10
Enter monthly insurance: $10
Enter monthly gas expanse: $10
Enter monthly oil change expense: $10
Enter monthly expense on tires: $10
Enter monthly cost on other maintenances: $10
Average yearly expense of automobile is $720.0.
Efficiency
You can achieve the same result with just one function.
def costs():
# Getting the cost of expenses from user "loanPayment, insurance, gas, oil, tires, maintainance."
loanPayment = float(input('Enter monthly payment: $'))
insurance = float(input('Enter monthly insurance: $'))
gas = float(input('Enter monthly gas expanse: $'))
oil = float(input('Enter monthly oil change expense: $'))
tires = float(input('Enter monthly expense on tires: $'))
maintenance = float(input('Enter monthly cost on other maintenances: $'))
# Calculate sum
total = loanPayment + insurance + gas + oil + tires + maintenance
## Monthly Cost
print('\n', f'Average monthly expense of automobile is ${total}.', '\n')
## Annual Cost
print(f'Average yearly expense of automobile is ${total * 12}.')
if __name__ == "__main__":
costs()
Result
This behaves like so:
Enter monthly payment: $15
Enter monthly insurance: $12
Enter monthly gas expanse: $13
Enter monthly oil change expense: $14
Enter monthly expense on tires: $15
Enter monthly cost on other maintenances: $14
Average monthly expense of automobile is $83.0.
Average yearly expense of automobile is $996.0.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
