0

我第一次使用python,我被这个臭名昭著的问题困住了,我一辈子都无法弄清楚它为什么不起作用。当我尝试运行我的程序时,我可以在不修改的情况下得到年度成本的答案(即使它是错误的,我也不知道为什么),但不能得到修改后的年度成本。

我已经尝试重写它以防我错过了冒号/括号/等,但这没有用,我尝试重命名它。我试着把它完全拿出来(这是我摆脱那个烦人的错误信息的唯一方法)

支付文件

from mpg import *

def main():
    driven,costg,costm,mpgbm,mpgam = getInfo(1,2,3,4,5)
    print("The number of miles driven in a year is",driven)
    print("The cost of gas is",costg)
    print("The cost of the modification is",costm)
    print("The MPG of the car before the modification is",mpgbm)
    print("The MPG of the car afrer the modification is",mpgam)

costWithout = getYearlyCost(1,2)
print("Yearly cost without the modification:", costWithout)

costWith = getYearlyCost2()
print("Yearly cost with the modification:", costWith)

虽然我知道其中有一个错误(很可能有很多错误),但我看不到它。有人可以向我指出并帮助我解决它吗?

我还添加了我的 mpg.py,以防错误在那里而不是支付文件。

def getInfo(driven,costg,costm,mpgbm,mpgam):
    driven = eval(input("enter number of miles driven per year: "))
    costg = eval(input("enter cost of a gallon of gas: "))
    costm = eval(input("enter the cost of modification: "))
    mpgbm = eval(input("eneter MPG of the car before the modification: "))
    mpgam = eval(input("enter MPG of the car after the modification: "))
    return driven,costg,costm,mpgbm,mpgam

def getYearlyCost(driven,costg):
    getYearlyCost = (driven / costg*12)
def getYealyCost2(driven,costm):
    getYearlyCost2 = (driven / costm*12)
    return getYearlyCost,getYearlyCost2

def gallons(x,y,z,x2,y2,z2):
    x = (driven/mpgbm)     # x= how many gallons are used in a year
    y = costg
    z = (x*y)               # z = how much money is spent on gas in year
    print("money spent on gas in year ",z)

    x2 = (driven/mpgam)     # x2 = how much money is spent with mod.
    z2 = (x2*y)
    y2 = (costm + z2)
                                                          1,1           Top
4

1 回答 1

4

这是您的直接问题:

costWith = getYearlyCost2()

您尝试调用的函数被命名getYealyCost2()(没有“r”)。

一旦你解决了这个问题,你就会发现其他问题,例如没有return语句getYearlyCost(),试图返回函数并getYearlyCost()在没有任何参数的情况下调用。getYearlyCost2()getYearlyCost2()

最重要的是,import *不赞成,然后使用eval()... 但这对于初学者来说是可行的。

于 2012-02-24T06:40:34.753 回答