简体   繁体   English

我在哪里弄乱输出格式?

[英]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.

相关问题 弄乱我的unicode输出-但是在哪里以及如何进行? - Messing up my unicode output - but where and how? 索引枚举似乎没有正常运行。 我哪里搞砸了? - Index Enumeration doesn't seem to be operating properly. Where am I messing up? Julia 与 Python 实现 https://projecteuler.net/problem=2。 这对我来说很奇怪,我不知道我在 Julia 哪里搞砸了 - Julia vs Python implementation for https://projecteuler.net/problem=2. This was very strange to me and I don't know where I am messing up in Julia Python:CSV模块。 EscapeChar弄乱了输出 - Python: CSV module. EscapeChar is messing up the output Python - 线程同时打印,弄乱了文本输出 - Python - Threads are printing at the same time messing up the text output 使用多重处理从数据帧写入csv而不弄乱输出 - Writing to csv from dataframe using multiprocessing and not messing up the output 我试图在 json 中输出,其中键等于特定值 - I am trying to output in json where a key is equals to a particular value 我怀疑re.match弄乱了控制流程 - I suspect that re.match messing up control flow 如何解决这个混淆了我的代码的 ValueError 消息? - How do I Solve this ValueError message that is messing up with my code? 如何从后台开始删除此脚本并弄乱 PowerShell? - How can I remove this script starting in the background and messing up with PowerShell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM