更正生成语法错误python 3.6.2的ELIF语句
使用IF、ELIF语句供用户选择排序算法对输入的列表进行排序。 Elif不断返回语法错误,但我不明白为什么。据我所知,它是正确缩进的,有一个冒号,并且我知道还有其他语法错误 代码:更正生成语法错误python 3.6.2的ELIF语句,python,Python,使用IF、ELIF语句供用户选择排序算法对输入的列表进行排序。 Elif不断返回语法错误,但我不明白为什么。据我所知,它是正确缩进的,有一个冒号,并且我知道还有其他语法错误 代码: if response == ("bubble"): bubble(numbers) elif response == ("insertion"): insertion(numbers) elif response == ("merge"): merge(numbers) elif respo
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
语句等的主体)出现时,再次取消缩进已完成。我可以执行此代码而不出现任何语法错误。发布a。向我们显示错误。我在插入的第一个elif中得到一个语法错误。是否可能我的代码是正确的,并且Python有一个错误试图执行它?从技术上讲,Python中可能有错误,但实际上-没有。您可能会有语法错误r在您的代码前面(请按照上面的要求发布MCVE),或者您在缩进中混合了制表符和空格。input(what algorithm,”)
是错误的。是的,谢谢。我很惊讶我能正确缩进所有算法,但一个简单的IF语句不正确。再次感谢您的帮助