python - Where am I messing up with output formatting? -
so got error message when tried run code , can't figure out problem is. says it's valueerror can't figure out 1 exactly. maybe it's late, @ loss.
here's code:
def sort(count_dict, avg_scores_dict, std_dev_dict): '''sorts , prints output''' menu = menu_validate("you must choose 1 of valid choices of 1, 2, 3, 4 \n sort options\n 1. sort avg ascending\n 2. sort avg descending\n 3. sort std deviation ascending\n 4. sort 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)) 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)) 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)) 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)) 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 output , error when run it:
you must choose 1 of valid choices of 1, 2, 3, 4 sort options 1. sort avg ascending 2. sort avg descending 3. sort std deviation ascending 4. sort 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 automatic field numbering manual field specification
what messing up? , appreciated!
you can't switch , forth between automatic field numbering - specifying simple {}
- , manual field specification, e.g. {0}
. if want same field repeated several times, 'word'
in example, you'll have specify want other fields be. example, might want start first argument, 'word'
, element 0
, , fifth argument, '='*51
, last, 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 decide arguments want placed in format string.
Comments
Post a Comment