indented block,應該怎麼辦? indentationerror:expected an
- IT科技
- 關注:2.26W次
這段代碼完全沒有問題,可能是你編輯器的問題,導致縮進對不上位置,另外我改寫了下你的程序 ====================guess.py======== number = 23 while True: guess = raw_input('Enter an integer('q' for quit) : ') #'q' for quit if guess
python編寫時報IndentationError:expected an indented block錯誤,這時候該怎麼辦呢?下面與大家分享解決方法
方法
編寫Python代碼時會遇到expected an indented block錯誤
這段代碼完全沒有問題,可能是你編輯器的問題,導致縮進對不上位置,另外我改寫了下你的程序 ====================guess.py======== number = 23 while True: guess = raw_input('Enter an integer('q' for quit) : ') #'q' for quit if guess
原因在於“:”後面的語句需要進行縮進
你的代碼應該不全吧。凡是循環內部都要縮進1個空格。如果你if else縮進了,但是while也是循環,也需要縮進!所以while內部的if裏需要縮進2個空格。你的錯誤就是循環沒有加空格引起的。 #!/usr/bin/pythonimport osls=os.linesepfname = raw_inpu
將print語句縮進後即可正確輸出a的值了
這段代碼完全沒有問題,可能是你編輯器的問題,導致縮進對不上位置,另外我改寫了下你的程序 ====================guess.py======== number = 23 while True: guess = raw_input('Enter an integer('q' for quit) : ') #'q' for quit if guess
擴展閲讀,以下內容您可能還感興趣。
很簡單的python程序有問題:錯誤提示:IndentationError:expected an indented block!
這段代碼完全沒百有問題,可度能是你編輯器的問問題,導答致縮進對不上位置,另外我改回寫了下你的程序答
====================guess.py========
number = 23
while True:
guess = raw_input('Enter an integer(\'q\' for quit) : ')
#'q' for quit
if guess == 'q':
break
guess = int(guess)
if guess == number:
print 'Congratulations, you guessed it.'
break
elif guess < number:
print 'No, it is a little higher than that'
else:
print 'No, it is a little lower than that'
python入門,請幫忙看一下這個程序問題在哪裏,IndentationError: expected an indented block。
你的e68a847a6431333363356630代碼應該不全吧。凡是循環內部都要縮進1個空格。如果你if else縮進了,但是while也是循環,也需要縮進!所以while內部的if裏需要縮進2個空格。你的錯誤就是循環沒有加空格引起的。
#!/usr/bin/pythonimport os
ls=os.linesep
fname = raw_input("please input file name:n")
#get filename
while True:
if os.path.exists(fname):
print ("ERROR:'%s' already exists"%fname)
else:
break
#get file content (text)lines
all=[]
print("nEnter line('.'by itself to quit).n")
while True:
entry=input('>')
if entry=='.':
break
else:
all.append(entry)
#write lines to file with proper line-ending
fobj=open(fname,'w')
fbj.writelines('%s%s'%(x,ls)for x in all)
fobj.close()
print('DONE')
#File "<input>", line 8
if os.path.exists(fname):
print "ok"
Python中def的錯誤,IndentationError: expected an indented block
因為這裏是給class定義方法,不是全局函數,所以應該空格。
python第二行有什麼問題 IndentationError: expected an indented block
縮進問題吧
python for循環
兄弟,你眼神呢?print前邊加4個空格or tab更多追問追答追問縮進了是這樣,敲一下回車到了下一行,並沒有打印執行的結果。
>>> for a in ['a','s','c']:
... print(a)
...追答兄弟,多打幾個回車試試可好,你現在還在代碼塊裏啊!!!其實咋倆水平差不多,我準備用Python做自動化滲透,剛開始準備學Ruby的,後來感覺這個比Ruby好點,就學這個了,你呢,追問我是初學者,不是print()之後敲回車會打印結果的嘛,就這個地方不行。感覺是版本或者環境問題。其他的都是好的。多謝了。追答沒事沒事,2.7好像也是一樣的,找個IDE吧,比黒框框好用,追問莫名其妙,我在第二行的地方敲了一個回車,然後就執行。
>>> for a in ['a','b','c']:
... print(a)
...
a
b
c追答他肯定要多留一行啊,要不你要是還要打代碼不就不能了,沒有返回代碼他就執行了嘛,
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zh-mo/dianzi/xwq9x8.html