因此,当我尝试运行代码时,我收到了一条错误消息(从previous question继续),我无法弄清楚究竟是什么问题。它说这是一个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}{1:27}{2:39}{3:51}\n{4}".format("Word", "Occurence", "Avg. Score", "Std. Dev.", "="*51))
if menu == 1:
dic = OrderedDict(sorted(word_avg_dict.items(), key=lambda x:x[1], reverse=False))
for key in dic:
print ("{0}{1:27}{2:39:.4f}{3:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key]))
elif menu == 2:
dic = OrderedDict(sorted(word_avg_dict.items(), key=lambda x:x[1], reverse=True))
for key in dic:
print ("{0}{1:27}{2:39:.4f}{3: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}{1:27}{2:39:.4f}{3: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}{1:27}{2:39:.4f}{3: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
WordOccurence Avg. Score Std. Dev.
===================================================
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 104, in sort
print ("{0}{1:27}{2:39:.4f}{3:51:.4f}".format(key, count_dict[key], avg_scores_dict[key], std_dev_dict[key]))
ValueError: Invalid format specifier
我搞砸了什么?任何和所有的帮助表示赞赏!
答案 0 :(得分:2)
您不应在单个规范字段中拥有多个冒号,例如long → 1234l
float → 25f
short → (short)5
double → (double)25
。删除第二个:
{3:51:.4f}