简体   繁体   English

无法获取此代码的 output。 任何帮助都会得到帮助

[英]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()

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.首先,您需要使用您创建的新变量在expenses() () 的末尾调用monthlyCost()

Second of all there is a typo in monthlyCost() where you use monthlyCost instead of totalMonthlyCost其次,在monthlyCost()中有一个拼写错误,您使用monthlyCost而不是totalMonthlyCost

Finally, you are calling yearlyCost() with no parameter, which you can simply remove beacause it will be called from monthlyCost()最后,您正在调用不带参数的yearlyCost() ,您可以简单地删除它,因为它将从monthlyCost()调用

I also added a ' $' to the end of the string in monthlyCost()我还在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()

Problems问题

  1. Local variables.局部变量。 When you define variables inside a function, you'd expect to be using them later within the function .当您在 function 中定义变量时,您希望稍后在 function中使用它们。 Otherwise, the values are not callable later on in your program unless you return them.否则,这些值以后在您的程序中不可调用,除非您return它们。 Which leads to problem 2...这导致问题 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.

Efficiency效率

You can achieve the same result with just one function.只需一个 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM