Home > Enterprise >  unable to get the output of this code. Any help will be appriciated
unable to get the output of this code. Any help will be appriciated

Time:02-21

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

CodePudding user response:

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

CodePudding user response:

Problems

  1. 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 return them. Which leads to problem 2...

  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.
  • Related