我是靠谱客的博主 爱笑云朵,最近开发中收集的这篇文章主要介绍elif函数python_关于python 3.x:函数if / elif / else语法错误,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我无法弄清楚为什么我写这个函数的语法错误。 它发生在第一个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语法错误所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部