我在哪里搞乱输出格式? [英] Where am I messing up with output formatting?
问题描述
这是我的代码:
def sort(count_dict,avg_scores_dict,std_dev_dict):
'''排序并打印输出'''
menu = menu_validate(您必须选择1的有效选项之一, 2,3,4 \ n排序选项\\\
1.按平均升序排序\ n 2.按平均降序排序\ 3.按标准偏差升序排序\ n 4.按标准偏差降序排序, 1,4)
print({} {0:27} {0:39} {0:51} \\\
{}格式(Word,Occurence,平均分数 ,Std。Dev。,=* 51))
if menu == 1:
dic = OrderedDict(sorted(word_average_dict.items(),key = lambda x: x $ [1],reverse = False))
为键盘输入:
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 = lamb da x:x [1],reverse = True))
for 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格式(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格式(key,count_dict [key],avg_scores_dict [key],std_dev_dict [key]))
返回无
这是我的输出和错误,当我运行它时:
您必须选择1,2,3,4
的有效选项之一排序选项
1.按平均升序排序
2.按Av排序g降序
3.按标准偏差升序
排序4.按标准偏差降序排序1
追溯(最近一次调用最后一个):
文件C:\ Users \Ryan \Documents\Program 7\Program 7.py,line 161,in< module>
output = sort(cnt_dict,word_avg_dict,std_dev_dict)
文件C:\ Users \Ryan\Documents\Program 7\Program 7.py,行99,in
print({} {0:27} {0:39} {0:51} \\\
{}格式(Word,Occurence,平均分数, ,=* 51))
ValueError:无法从自动字段编号切换到手动字段指定
我在搞什么?任何和所有的帮助,赞赏!
你不能在自动字段编号之间来回切换 - 一个简单的 {}
- 和手工字段说明,例如 {0}
。如果你想多次重复同一个字段,如'Word'
在你的例子中,你还必须指定你想要的其他字段。例如,你可能想从第一个参数'Word'
,它是元素 0
开始,第五个参数,'='* 51
,作为最后一个元素 4
:
>>> print({0} {0:27} {0:39} {0:51} \\\
{4}。格式(Word,Occurence,平均得分, ,=* 51))
WordWord Word Word
========================== =========================
您必须为您自己决定要将哪些参数放在格式字符串中的位置。
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. 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, e.g. {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. 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
:
>>> 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.
这篇关于我在哪里搞乱输出格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!