2016-04-25 2 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'があるので、他のフィールドを何にするかを指定する必要があります。たとえば、要素4ある、最後として、要素0、および5番目の引数、'='*51で最初の引数、'Word'、で開始することがあります:

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

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

あなたがのために決定する必要がありますどの引数を書式文字列のどこに配置するかを指定します。

関連する問題