2012-02-24 139 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() ...的使用,但這將爲首發。