2016-04-25 76 views
0

我想不通爲什麼我得到這個函數的語法錯誤,我寫了。它發生在第一個elif。當我嘗試運行它時,IDLE中的錯誤僅表示「無效語法」,然後突出顯示「elif」。函數if/elif/else語法錯誤

驗證碼:

def sort(count_dict, avg_scores_dict, std_dev_dict): 
    '''sorts and prints the output''' 
    menu = menu_validate("You must choose one of the valid choices of 1, 2, 3, 4 \n  Sort Options\n 1. Sort by Avg Ascending\n 2. Sort by Avg Descending\n 3. Sort by Std Deviation Ascending\n 4. Sort by Std Deviation Descending", 1, 4) 
    print ("{}{0:27}{0:39}{0:51}\n{}".format("Word", "Occurence", "Avg. Score", "Std. Dev.", "="*51)) 

    if menu == 1:  
     dic = OrderedDict(sorted(word_average_dict.items(), key=lambda x:x[1], reverse=False)) 
     for key in dic: 
      print ("{}{0:27}{0:39:.4f}{0:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key]) 

    elif menu == 2: 
     dic = OrderedDict(sorted(word_average_dict.items(), key=lambda x:x[1], reverse=True)) 
     for key in dic: 
      print ("{}{0:27}{0:39:.4f}{0:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key]) 

    elif menu == 3: 
     dic = OrderedDict(sorted(std_dev_dict.items(), key=lambda x:x[1], reverse=False)) 
     for key in dic: 
      print ("{}{0:27}{0:39:.4f}{0:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key]) 

    elif menu == 4: 
     dic = OrderedDict(sorted(std_dev_dict.items(), key=lambda x:x[1], reverse=True)) 
     for key in dic: 
      print ("{}{0:27}{0:39:.4f}{0:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key]) 

    return None 

此外,沒有我的打印格式看起來是正確的?它應該證明這麼多的空間是正確的,最後2個是浮點數到小數點後第四位。

任何幫助,非常感謝!

+0

這是一個錯字。您沒有在第9行的印刷聲明中關閉最後一個圓括號。 – Roberto

+0

您是先生,是救命恩人! –

回答

0

看起來你正在每個elif的開始之間輸入一個空行。

試試這個:

def sort(count_dict, avg_scores_dict, std_dev_dict): 
    '''sorts and prints the output''' 
    menu = menu_validate("You must choose one of the valid choices of 1, 2, 3, 4 \n  Sort Options\n 1. Sort by Avg Ascending\n 2. Sort by Avg Descending\n 3. Sort by Std Deviation Ascending\n 4. Sort by Std Deviation Descending", 1, 4) 
    print ("{}{0:27}{0:39}{0:51}\n{}".format("Word", "Occurence", "Avg. Score", "Std. Dev.", "="*51)) 

    if menu == 1:  
     dic = OrderedDict(sorted(word_average_dict.items(), key=lambda x:x[1], reverse=False)) 
     for key in dic: 
      print ("{}{0:27}{0:39:.4f}{0:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key])) 
    elif menu == 2: 
     dic = OrderedDict(sorted(word_average_dict.items(), key=lambda x:x[1], reverse=True)) 
     for key in dic: 
      print ("{}{0:27}{0:39:.4f}{0:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key])) 
    elif menu == 3: 
     dic = OrderedDict(sorted(std_dev_dict.items(), key=lambda x:x[1], reverse=False)) 
     for key in dic: 
      print ("{}{0:27}{0:39:.4f}{0:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key])) 
    elif menu == 4: 
     dic = OrderedDict(sorted(std_dev_dict.items(), key=lambda x:x[1], reverse=True)) 
     for key in dic: 
      print ("{}{0:27}{0:39:.4f}{0:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key])) 
    return None 
+0

是一個簡單的錯字。修復。 –

+0

@ryandoucette太棒了!如果這個答案有效,請接受這個答案。謝謝! :) – Edward