[英]else if and elif not working for my python code
這是我學習編碼的第一天。 我試過這段代碼,不知道為什么會失敗。 任何幫助深表感謝:
a=int(input())
if a>10:
print("your number is greater than 10")
else:
我得到SyntaxError: invalid syntax
if a>10:
print("your number is greater than 10") #indent the print statement
else:
print('Not greater than 10')
#you need to perform an action inside the else condition
您的縮進不正確。 它應該如下所示:
a=input()
if a>10:
print("your number is greater than 10")
else:
# You didn't have anything in your else statement so either remove it or add a statement...
print("your number is <= 10")
請參閱有關縮進的 python 文檔。
你有一個縮進錯誤:
if condition:
code...
elif condition:
another code...
else:
last code...
其中elif
和else
部分是可選的, elif
表示else: if condition:
。
PS:使用int(input())
否則你會得到一個字符串但沒有一個 int。
使用 control-j 和 control-left-bracket 進行縮進,使用 control-right-bracket 進行 dedent 將使您重新控制縮進。 當您在解釋器中鍵入行時,它會錯誤地管理縮進。 由您決定是否正確。
因此,您正在關注的非正式教程需要更新。 它可能對舊版本的 Python “有效”。 讓它適用於所有人將是一項任務,因為這些年來語言已經發生了變化。
兄弟,縮進在Python中非常重要。 由於 else: 是另一個條件表達式,它應該獨立於任何其他條件表達式。 因此,它必須放在 if a>10: 的縮進之外,並且里面應該有一個語句,它將在滿足它所屬的上述條件時執行。 因此,正確的代碼是:-
a=int(input())
if a>10:
print("your number is greater than 10")
else:
print("your number is not greater than 10")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.