raw_input()没有正常工作?
好的,
我现在正在用Python做一个简单的文字冒险游戏。不过,有一个函数在读取输入时出现了奇怪的问题。
目前,每个地下城的房间都是一个单独的函数。这里是那个不工作的房间:
def strange_room():
global fsm
global sword
global saw
if not fsm:
if not saw:
print "???..."
print "You're in an empty room with doors on all sides."
print "Theres a leak in the center of the ceiling... strange."
print "In the corner of the room, there is an old circular saw blade leaning against the wall."
print "What do you want to do?"
next6 = raw_input("> ")
print "next6 = ", next6
if "left" in next6:
zeus_room()
elif "right" in next6:
hydra_room()
elif "front" or "forward" in next6:
crypt_room()
elif ("back" or "backwad" or "behind") in next6:
start()
elif "saw" in next6:
print "gothere"
saw = True
print "Got saw."
print "saw = ", saw
strange_room()
else:
print "What was that?"
strange_room()
if saw:
print "???..."
print "You're in an empty room with doors on all sides."
print "Theres a leak in the center of the ceiling... strange."
print "What do you want to do?"
next7 = raw_input("> ")
if "left" in next7:
zeus_room()
elif "right" in next7:
hydra_room()
elif "front" or "forward" in next7:
crypt_room()
elif ("back" or "backwad" or "behind") in next7:
start()
else:
print "What was that?"
strange_room()
我的问题出在获取输入上。这个函数执行到第17行时就出问题了。第一次运行时,它似乎能接收到输入,但用来打印输入的那条语句却没有执行。而且,除了左、右和前进的指令能正常工作外,其他我输入的内容都只会执行“前进”应该执行的crypt_room()函数。
谢谢。
2 个回答
0
Sven Marnach 说了为什么你的代码不管用。要让它正常工作,你应该使用 any()
函数:
("back" or "backwad" or "behind") in next6:
应该改成
any(direction in next6 for direction in ("back", "backwad", "behind")):
4
这个表达式
"front" or "forward" in next6
会得到结果 "front"
,并且在 if
语句中总是被认为是“真”的。你可能想表达的是
"front" in next6 or "forward" in next6
你的代码中还有更多类似的错误。一般来说,表达式
A or B
如果 A
是被认为“真实”的(可以理解为有意义的),那么结果就是 A
;如果不是,那结果就是 B
。
顺便提一下,你的程序整体设计有问题。当你进入不同的房间时,递归调用会很快达到最大递归深度。