ValueError:对关闭的文件执行I/O操作初学者处理多个文件

2024-04-27 03:29:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习python(2.7),正在制作一个简单的文本迷宫游戏,在不同的文件上有多个级别:

try: #this one works perfectly, it runs the file until it quits with quit()
  execfile("....Lockdown.py")
except SystemExit:
  print "Next Level"

try: #this one doesn't work even though it is almost perfectly identical with Lockdown
  execfile("....Lockdown2.py")
except SystemExit:
  print "Next Level"

#other levels

当它运行Lockdown2.py时

Traceback (most recent call last):
  File LockdownGame.py ...
  #-> until execfile("....Lockdown2.py")
  File "...Lockdown2.py", line 708, in available  #available is another function
decide(raw_input("Where do you decide to head? "))

ValueError: I/O operation on closed file

Lockdown2(和Lockdown)是这样工作的

#the building of the maze
def available(): #checks where the user can walk
    #looks through the entire maze to look at where the user is standing, where his back is and if there are walls in that direction
    #makes sure the user isn't facing
    print "You can walk %s." % (can_walk[0]) #cutting out how it tells this
    decide(raw_input("What direction do you decide to head? "))

def decide(dec):
    if dec == "left": #and the other three directions, 
        #edits the maze and their surrounding to their new location
   else:
      print "(Write your option as it's written)"
      decide(raw_input("What direction do you decide to head? "))

available()

它在放置raw_input之后崩溃,即使它实际上是与locdown相同的代码,并且在此之前的打印中,或者当这个原始输入处于locdown时,它不会出错。你知道吗


Tags: thetopyinputrawisitthis