2016-11-18 54 views
0

所以當我運行這個腳本時,它一直說'銷售未定義'。我只是錯過了一些明顯的東西?我是python的新手。爲了更具體一些,它說明了我們'上市(銷售,所有者)'的問題。我感謝任何幫助我的人,我希望你有愉快的一天。在一個函數定義Python 3 - 爲什麼這個變量沒有在這個函數中定義

def launch(): 
    sales = [0] * 7 
    sales[0] = float(125900) 
    sales[1] = float(115000) 
    sales[2] = float(105900) 
    sales[3] = float(85000) 
    sales[4] = float(150000) 
    sales[5] = float(155249) 
    sales[6] = float(97500) 
    owner = [0] * 7 
    owner[0] = "Carson" 
    owner[1] = "Smith" 
    owner[2] = "Jackson" 
    owner[3] = "Swanson" 
    owner[4] = "Perry" 
    owner[5] = "Beufort" 
    owner[6] = "Anderson" 
    return sales, owner 
def listing(sales, owner): 
    count = 0 
    count2 = 1 
    while count < 7: 
     print(count2 , "" , ":" , "" , "owner[count]\t" , "$" , "" , format(sales[count],',.2f')) 
     count = count + 1 
     count2 = count2 + 1 
def main(): 
    print("Welcome to the Botany Bay home sales calculator") 
    print("This program will calculate the average selling price of the homes") 
    print("sold this past year. It will then determine how many homes sold") 
    print("above the average and how many homes sold below the average.") 
     print("=======================================================================") 
    print("") 
    print("Botany Bay Home Sales") 
    print("*********************************") 
    listing(sales, owner) 

launch() 
main() 

回答

0

名稱是局部的作用,只有在他們裏面可見。呼喚:

listing(sales, owner) 

main將失敗,因爲這些名字是不是launch外部不可見。

你需要從launchsalesowner返回的值,並將其傳遞給main。因此,最好將main定義爲接受兩個參數。

def main(sales, owner): 
    # rest as is 

然後,值傳遞給main

sales, owner = launch() 
main(sales, owner) 

或者,因爲你可能不希望salesowner這裏要約束:

main(launch()) 

你們不要,如果你綁定名稱salesowner附:

sales, owner = launch() 

由於名稱是全局綁定的(即listing(sales, owner)會找到它們),因此您無需定義main帶兩個參數。我真的不會建議這樣做;將它們作爲參數傳遞給它們比離開它們更好(並且更快)。

0

sales在您的mains函數的本地範圍內沒有聲明。 你可以做的是改變你的主要功能是什麼讓2個參數是這樣的:

def main(sales, owner): 
    #do your stuff 

然後使用由launch()返回的兩個對象如下

main(*launch()) 

當明星是用來「解包「分成兩個對象,實際上由launch()返回的只有一個,即launch()返回(<type 'list'>, <type 'list'>),即包含兩個對象的元組。 *launch()返回此元組的解壓縮版本。

所以總結一下你的整個的代碼是(測試)

def launch(): 
    sales = [0] * 7 
    sales[0] = float(125900) 
    sales[1] = float(115000) 
    sales[2] = float(105900) 
    sales[3] = float(85000) 
    sales[4] = float(150000) 
    sales[5] = float(155249) 
    sales[6] = float(97500) 
    owner = [0] * 7 
    owner[0] = "Carson" 
    owner[1] = "Smith" 
    owner[2] = "Jackson" 
    owner[3] = "Swanson" 
    owner[4] = "Perry" 
    owner[5] = "Beufort" 
    owner[6] = "Anderson" 
    return sales, owner 
def listing(sales, owner): 
    count = 0 
    count2 = 1 
    while count < 7: 
     print(count2 , "" , ":" , "" , "owner[count]\t" , "$" , "" , format(sales[count],',.2f')) 
     count = count + 1 
     count2 = count2 + 1 
def main(sales, owner): 
    print("Welcome to the Botany Bay home sales calculator") 
    print("This program will calculate the average selling price of the homes") 
    print("sold this past year. It will then determine how many homes sold") 
    print("above the average and how many homes sold below the average.") 
    print("=======================================================================") 
    print("") 
    print("Botany Bay Home Sales") 
    print("*********************************") 
    listing(sales, owner) 


main(*launch()) 
相關問題