1 print("hello world")
2 myname = input("what is your name?")
3 print(myname)
4 if(myname == "ofir"):
5 print("you are great")
這是我用Python編寫的代碼(我今天才剛剛開始)。我得到這個錯誤「預期的縮進塊」中的第5行,我不知道我的錯。 謝謝你的幫助。我使用3.4.3 python。錯誤與打印或如果蟒蛇
1 print("hello world")
2 myname = input("what is your name?")
3 print(myname)
4 if(myname == "ofir"):
5 print("you are great")
這是我用Python編寫的代碼(我今天才剛剛開始)。我得到這個錯誤「預期的縮進塊」中的第5行,我不知道我的錯。 謝謝你的幫助。我使用3.4.3 python。錯誤與打印或如果蟒蛇
Python的要求下if
語句縮進,以及for
和while
循環,functions
,classes
等你似乎缺少縮進。我會考慮通過Codecademy互動教程去爲Python
print("hello world")
myname = input("what is your name?")
print(myname)
if myname == "ofir":
print("you are great")
你應該縮進代碼4個空格行,如果它的代碼塊的一部分。在Python中,這將包括if語句,循環,def和類。我建議你閱讀這篇文章:Python-Indetation。要回答您的問題,請將您的代碼更改爲:
print("hello world")
myname = input("what is your name?")
print(myname)
if(myname == "ofir"):
print("you are great") # note the 4 spaces
您需要在if語句後縮進。
並取消代碼前三行的縮進。
在C編程語言中,一段代碼(指令)包含在括號中。縮進無關緊要。在Pascal中,塊被包含在'begin''end'中。正如您現在在Python中已經注意到的,「if」,「else」,「for」,「while」,「def」等語句中包含的指令塊不包含任何內容。 Python知道如果幾個指令具有相同的縮進,則它們是同一個塊。
縮進可以是任意數量的空格或製表符,並且可以在Python腳本中變化。唯一的規則是在同一個塊內必須是相同的縮進。
請參閱與Python 2.7波紋管檢查代碼:
a = [1, 2, 3]
for e in a :
if e % 2 == 0 : # indent with 2 spaces
print "%d is even" % e # indent with 4 spaces
print
else : # indent with 2 spaces
print "%d is odd" % e # indent with 3 spaces
print
1 print("hello world")
2 myname = input("what is your name?")
3 print(myname)
4 if(myname == "ofir"):
5 print("you are great")
您需要縮進print("you are great")
爲了使程序編譯
要多少[Python的教程]的(HTTPS: //docs.python.org/3/tutorial/)你有跟着嗎?縮進是該語言的基礎*,並在[介紹部分](https://docs.python.org/3/tutorial/introduction.html)中進行了解釋。您似乎有* no *縮進,所以Python無法確定'if'塊的代碼部分開始或結束的位置。 –
我跟着2個python教程和第三個教程的一半。 –
去http://learnpythonthehardway.org/book/我認爲這對你更好。 –