2016-09-06 37 views
1

我只是在Python中使用函數(僅僅學習了6個月的Python),而且我被一些代碼無法工作。它說總數沒有定義,nameerror。通過閱讀一些帖子,我想我需要將總數存儲在一個變量中,但我不知道在哪裏。你可以在回報聲明中這樣做嗎?不確定在何處定義總數以使其成爲全局。在變量中捕獲結果

這是一個包含多個任務的程序。我也在努力將表存儲到csv文件中。這是代碼。

import csv 

def set_values(): 
    ans1 = float(input('Please enter the first number: ')) 

    ans2 = float(input('Please enter the second number: ')) 

    ans3 = float(input('Please enter the third number: ')) 

    levels = int(input('Please set the amount of levels between 5 and 10: ')) 


    return (ans1, ans2, ans3, levels) 


def display_model_values(ans1, ans2, ans3, levels): 
    print('The outcome for model 1 is ',ans1) 
    print('The outcome for model 2 is ',ans2) 
    print('The outcome for model 3 is ',ans3) 
    print('The number of levels are ',levels) 


def run_model(ans1, ans2, ans3, levels): 
    total = ans1+ans2+ans3 
    print ("\t","Level","\t","Answer 1","\t","Answer 2","\t","Answer 3","\t","Total") 
    for i in range (0,levels+1): 
     print("\t",i,"\t\t",ans1,"\t\t",ans2,"\t\t",ans3,"\t\t",total) 
     result1 =ans2*ans3 
     result2 = ans2/ans1 
     total = ans1+result1+result2 
    return (i,result1, result2, total) 


def export_data(ans1,ans2,ans3,total): 
    table = [ans1, ans2, ans3,total] 

    nameoffile = input('what would you like to call the filename') 
    nameoffile = open(nameoffile+".csv","w") 
    csv_file_ref = csv.writer(nameoffile) 
    csv_file_ref.writerow(table) 
    nameoffile.close() 

## with open(nameoffile+'.csv', 'w') as csvfile: 
##  writer = csv.writer(csvfile) 
##  writer.writerow(r) for r in table] 




choice = '' 
count = 0 
while choice != 'q': 
    print('Main Menu') 
    print ('1)Set Model Values') 
    print ('2)Display Model Values') 
    print ('3)Run Model') 
    print ('4)Export Data') 
    print ('Q)Quit') 
    choice = input('Please Enter Choice') 


    if choice =='1': 
     ans1, ans2, ans3, levels = set_values() 
     count = count +1 

    elif choice == '2': 
     if count < 1: 
      print ('you need to choose option 1 first') 
     else: 
      display_model_values(ans1,ans2,ans3,levels) 


    elif choice =='3': 
     if count < 1: 
      print('you need to choose option 1 first') 
     else: 
      run_model(ans1,ans2,ans3,levels) 

    elif choice =='4': 
     if count < 1: 
      print ('you need to choose option 1 first') 
     else: 
      export_data(ans1,ans2,ans3,total) 

    elif choice == 'Q': 
      break 
    else: 
     print('not an option') 
+1

請寄出'NameError'異常的回溯。 – FamousJameous

回答

1

您的總計僅在函數run_model中定義。當該函數返回時,您不能再次引用total,因爲它已不存在out of scope。變量現在是「未綁定」的,而這正是Python告訴你,當它說名稱total沒有定義。它被定義了一次,在某個地方,但它已經消失了。

一個簡單的改變你的代碼將有總重計算,出口函數體內部,如下:

def export_data(ans1,ans2,ans3): 
    total = ans1 + ans2 + ans3 # total is available inside of export_data 
    table = [ans1, ans2, ans3,total] 

    nameoffile = input('what would you like to call the filename') 
    nameoffile = open(nameoffile+".csv","w") 
    csv_file_ref = csv.writer(nameoffile) 
    csv_file_ref.writerow(table) 
    nameoffile.close() 

這些應該使你的代碼工作。

+0

謝謝,但是錯誤是在第87行,這是在ELIF選擇== 4 –

+0

還沒有工作,對不起。我確實添加了上述內容。我沒有得到如何返回它的全部無約束力。也許我不明白實際回報是什麼? –

+0

@LisaG對不起,上面的函數簽名有一個小錯誤。而不是'export_data(ans1,ans2,ans3,total)'它應該讀取'export_data(ans1,ans2,ans3)'。代碼現在編輯 – dangom

1

在線路83,您要發送的可變total給函數export_data未while循環內定義(ANS1,ANS2,ANS3,總計)。假設你的總量ans1+ans2+ans3發送值之前, 添加一行,

total = ans1+ans2+ans3 

這應該解決的問題。

+0

嗨,詹姆斯,謝謝。錯誤是 –

+0

回溯(最近通話最後一個): 文件 「E:\ test.py」,第87行,在 export_data(ANS1,ANS2,ANS3,總計) NameError:名字 '總' 沒有定義 –

+0

@LisaG,謝謝!你可以請我的答案upvote並接受答案。 –

0

DEF export_data(ANS1,ANS2,ANS3): 總= ANS1 + ANS2 + ANS3#總可用export_data 表= [ANS1,ANS2,ANS3,總]的內部

nameoffile = input('what would you like to call the filename') 
nameoffile = open(nameoffile+".csv","w") 
csv_file_ref = csv.writer(nameoffile) 
csv_file_ref.writerow(table) 
nameoffile.close() 

現在它出口並且沒有總計錯誤