概述
我无法弄清楚为什么我写这个函数的语法错误。 它发生在第一个elif。 当我尝试运行它时,IDLE中的错误只显示"无效语法"然后突出显示'elif'。
代码:
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
Sort Options
1. Sort by Avg Ascending
2. Sort by Avg Descending
3. Sort by Std Deviation Ascending
4. Sort by Std Deviation Descending", 1, 4)
print ("{}{0:27}{0:39}{0:51}
{}".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
另外,我的打印格式是否正确? 它应该证明在这么多空间上是合理的,最后2个是浮点数到小数点后4位。
任何帮助是极大的赞赏!
这是一个错字。 您没有在第9行关闭print语句的最后一个括号。
先生,你是救星!
看起来你在每个elif的开头之间进入一个空白行。
试试这个:
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
Sort Options
1. Sort by Avg Ascending
2. Sort by Avg Descending
3. Sort by Std Deviation Ascending
4. Sort by Std Deviation Descending", 1, 4)
print ("{}{0:27}{0:39}{0:51}
{}".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
这是一个简单的错字。 固定它。
@ryandoucette太棒了! 如果这个答案有效,请接受这个答案。 谢谢!:)
最后
以上就是爱笑云朵为你收集整理的elif函数python_关于python 3.x:函数if / elif / else语法错误的全部内容,希望文章能够帮你解决elif函数python_关于python 3.x:函数if / elif / else语法错误所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复