[英]Where am I messing up with output formatting?
So I got an error message when I tried to run my code and I can't figure out what exactly the problem is. 因此,当我尝试运行代码时收到错误消息,但我无法弄清楚到底是什么问题。 It says it's a ValueError but I can't figure out which one exactly. 它说这是一个ValueError,但我不知道到底是哪一个。 Maybe it's just late, but I am at a loss. 也许已经晚了,但我很茫然。
Here's my code: 这是我的代码:
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
Here's my output and error when I run it: 这是我运行时的输出和错误:
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
What am I messing up? 我搞砸了吗? Any and all help is appreciated! 任何和所有帮助表示赞赏!
You can't switch back and forth between automatic field numbering - what you get by specifying a simple {}
- and manual field specification, eg {0}
. 您无法在自动字段编号(通过指定简单的{}
)和手动字段指定(例如{0}
之间来回切换。 If you want the same field repeated several times, as 'Word'
is in your example, you'll have to also specify what you want the other fields to be. 如果您希望同一字段重复多次,例如您的示例中的'Word'
,则还必须指定其他字段的内容。 For example, you might want to start with the first argument, 'Word'
, which is element 0
, and the fifth argument, '='*51
, as the last, which is element 4
: 例如,您可能要以第一个参数'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
===================================================
You'll have to decide for yourself which arguments you want to be placed where in the format string. 您必须自己决定要将哪些参数放在格式字符串中的什么位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.