[英]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.