我正在学习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
后崩溃,即使它与Lockdown几乎完全相同,并且在此之前打印时没有错误,或者当raw_input处于锁定状态时。
答案 0 :(得分:0)
将quit()更改为sys.exit(),它会在启动Lockdown2之前完全关闭Lockdown