[英]Correct ELIF statement producing a syntax error 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
语句等)完成时再次取消缩进.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.