使用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")
(我的其余代码只是四种排序算法,如果它们是注释,我认为它们不相关,我将对其进行编辑。
答案 0 :(得分:0)
您的缩进确实存在问题。您已使大多数代码缩进了一个等级,超出了应有的等级。尝试以下方法:
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
语句的主体等) 。)完成。