2016-11-18 7 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

関数で定義された名前は、その関数に対してローカルであり、その関数内でのみ表示されます。呼び出し:それらの名前はlaunchの外には見えませんので、main

listing(sales, owner) 

は失敗します。

salesownerの戻り値をlaunchから取得し、mainに渡す必要があります。したがって、mainを2つの引数を受け入れるように定義する方が良いです。

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

mainに値を渡す:

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

それとも、あなたはsalesownerがここに束縛されたくない場合がありますので、:あなたが結合した場合

main(launch()) 

はということではない取りますsalesownerの名前:

sales, owner = launch() 

名前がグローバルにバインドされているため(つまりlisting(sales, owner)が見つかるため)、2つの引数を使用してmainを定義する必要はありません。私は本当にそれを示唆していないでしょう。それをそれらのように残すよりもargsとして渡す方が良い(そして少し速く)。

0

salesは、宣言されていない関数mainsのローカルスコープにあります。 何あなたができることは、このような2つの引数を取得するためにあなたの主な機能を変更している。

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

をそして星は「解凍するために使用される

main(*launch()) 

を次のようにlaunch()によって返された2つのオブジェクトを使用します"launch()によって実際に唯一として返されるもの、つまりlaunch()が2つのオブジェクトに含まれるタプル、すなわち(<type 'list'>, <type 'list'>)を返す2つのオブジェクトに変換します。 *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()) 
関連する問題