2016-04-25 71 views
0

因此,當我嘗試運行我的代碼時,我收到了一條錯誤消息,我無法弄清楚究竟是什麼問題。它說這是一個ValueError,但我無法確切知道哪一個。也許這只是晚了,但我不知所措。我在哪裏搞亂輸出格式?

這裏是我的代碼:

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 

這裏是我的輸出和錯誤,當我運行它:

You must choose one of the valid choices of 1, 2, 3, 4 
     Sort Options 
    1. Sort by Avg Ascending 
    2. Sort by Avg Descending 
    3. Sort by Std Deviation Ascending 
    4. Sort by Std Deviation Descending1 
Traceback (most recent call last): 
    File "C:\Users\Ryan\Documents\Program 7\Program 7.py", line 161, in <module> 
    output = sort(cnt_dict, word_avg_dict, std_dev_dict) 
    File "C:\Users\Ryan\Documents\Program 7\Program 7.py", line 99, in sort 
    print ("{}{0:27}{0:39}{0:51}\n{}".format("Word", "Occurence", "Avg. Score", "Std. Dev.", "="*51)) 
ValueError: cannot switch from automatic field numbering to manual field specification 

什麼我搞亂?任何和所有的幫助表示讚賞!

回答

3

您不能在自動字段編號 - 您通過指定簡單的{} - 和手動字段規範(例如, {0}。如果你想多次重複相同的字段,如'Word'在你的例子中,你還必須指定你想要的其他字段。例如,你可能要開始的第一個參數,'Word',這是件0,而第五個參數,'='*51,作爲最後一個,這就是元4

>>> print("{0}{0:27}{0:39}{0:51}\n{4}".format("Word", "Occurence", "Avg. Score", "Std. Dev.", "="*51)) 
WordWord      Word         Word 

=================================================== 

你必須決定你自己要把哪些參數放在格式字符串的哪裏。