2012-02-24 142 views
0

我第一次使用python,并且我被困在这个恶臭问题上,并且不能为我的生活找出为什么它不工作。当我尝试运行我的程序时,我可以在没有修改的情况下得到年度成本的答案(即使它错了,我不知道为什么),但不是修改后的年度成本。全球名称'getYearlyCost2'未定义

我试过重写它,以防万一我错过了冒号/括号/等,但没有奏效,我试着重命名它。我试图把它完全摆脱了(这是我可以摆脱烦人的错误消息的唯一途径)

回报文件

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

这是你直接的问题:

costWith = getYearlyCost2() 

你想调用的函数被命名为getYealyCost2()(没有 “R”)。

还有,你很快就会为你修复发现其他问题,如在getYearlyCost()没有return声明,并试图在getYearlyCost2()返回函数getYearlyCost(),并呼吁getYearlyCost2()不带任何参数。

最重要的是,import *是皱眉,然后有eval() ...的使用,但这将为首发。