python入门,请帮忙看一下这个程序问题在哪里,IndentationError: expected an indented block。

2025-03-09 10:18:43
推荐回答(2个)
回答1:

你的代码应该不全吧。凡是循环内部都要缩进1个空格。如果你if else缩进了,但是while也是循环,也需要缩进!所以while内部的if里需要缩进2个空格。你的错误就是循环没有加空格引起的。

#!/usr/bin/python
import 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 "", line 8
if os.path.exists(fname):
 print "ok"

回答2:

无语 。你将每8行的IF和ELSE左边加两个空格就好了。