【发布时间】:2013-08-22 04:17:53
【问题描述】:
我正在写一个糟糕的基于文本的冒险,但我不知道如何打破这个循环。我将尝试在此处发布相关内容。如果我的 cmets 没有充分解释我想要做什么,请告诉我。
chained = 1
finished = 0
# home() and club() act as rooms in the game
def home():
while chained == 1:
# there's a way to unchain yourself that works
chained = 0
while chained == 0:
print 'your are in the room'
input = raw_input('> ')
if 'exit' in input or 'leave' in input or 'club' in input:
current_room = 'club'
break
def club():
print "this works"
# These sort of move you around the rooms.
# current_room keeps track of what room you're in
current_room = 'home'
while finished == 0:
if current_room == 'home':
home()
if current_room == 'club':
club()
预期的行为是我会在输入中输入“exit”或“leave”或“club”,home() 函数将结束,club() 函数将启动。实际发生的情况是终端不断打印“你在房间里”并不断给我输入。
如果必须,我会发布我完全未删节的代码,但我不想这样做,因为实际的冒险并不完全...专业。
【问题讨论】:
-
现在,如果 current_room == 'club': club(),这条错误线会做什么
-
current_roominhome()是与主作用域中的current_room不同的变量。您正在设置该函数特定的值,但它永远不会更新顶级范围内具有相同名称的其他变量。 -
@Haidro 'finished' 是一个变量,它可以保持循环以移动房间。基本上,每次你改变房间时,你都会改变房间变量,停止函数,然后检查房间变量,然后启动那个房间的函数。编辑了主帖。
标签: python while-loop