更正 ELIF 语句产生语法错误 python 3.6.2

亚历克斯布莱克

使用 IF、ELIF 语句让用户选择排序算法对输入的列表进行排序。Elif 不断返回语法错误,但我不明白为什么。据我所知,它已正确缩进,存在冒号,并且我知道没有其他语法错误。

代码:

if response == ("bubble"):
    bubble(numbers)
elif response == ("insertion"):
    insertion(numbers)
elif response == ("merge"):
    merge(numbers)
elif response == ("quick"):
    quick(numbers)
else:
    print("incorrect response")

MCVE:

numbers = [int(x) for x in input("input your list ").split()]
response = input(what algorithm, ")
    if response == ("bubble"):
        bubble(numbers)
    elif response == ("insertion"):
        insertion(numbers)
    elif response == ("merge"):
        merge(numbers)
    elif response == ("quick"):
        quick(numbers)
    else:
        print("incorrect response")

(我的其余代码只是四种排序算法,我认为它们不相关,如果它们是注释,我将对其进行编辑。

罗宾·齐格蒙德

你的缩进确实有问题。这段代码的大部分缩进都超出了它应有的水平。试试这个:

numbers = [int(x) for x in input("input your list ").split()]
response = input(what algorithm, ")
if response == ("bubble"):
    bubble(numbers)
elif response == ("insertion"):
    insertion(numbers)
elif response == ("merge"):
    merge(numbers)
elif response == ("quick"):
    quick(numbers)
else:
    print("incorrect response")

Python 中的缩进表示“块”的内部,在许多其他语言中它被放在花括号之间。本质上,规则是(毫无疑问,有一些例外,我正在掩盖) - 总是在冒号后缩进,并在“块”(函数/循环/类/if语句等的主体)完成时再次取消缩进

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章