函数if / elif / else语法错误

时间:2016-04-25 03:24:37

标签: function python-3.x if-statement

我无法弄清楚为什么我写这个函数会出现语法错误。它发生在第一个elif。当我尝试运行它时,IDLE中的错误只显示“无效语法”然后突出显示'elif'。

守则:

names <- c("sdadohf abc", "fsdgodhgf abc", "afhk xyz", "aksle   abc")

另外,我的打印格式是否正确?它应该证明在这么多空间上是合理的,最后2个是浮动到小数点后4位。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

似乎你在每个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 \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