Python Can';不要打破while循环

Python Can';不要打破while循环,python,while-loop,Python,While Loop,我正在写一篇糟糕的基于文本的冒险,我不知道如何打破这个循环。我会试着把相关的东西贴在这里。请告诉我,如果我的评论没有充分解释我想做什么 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

我正在写一篇糟糕的基于文本的冒险,我不知道如何打破这个循环。我会试着把相关的东西贴在这里。请告诉我,如果我的评论没有充分解释我想做什么

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()函数将启动。实际上,终端只是不断地打印“你在房间里”,并不断地给我输入


如果必须的话,我会发布我完全未删节的代码,但我宁愿不发布,因为实际的冒险并不完全……专业。

break所做的是在
home()
函数中打破循环。因此,当这种情况发生时,它将回到本世纪初

while finished == 0:
并将继续重复该输入

您还必须在
home()
(和
club()
)之后提供
break


顺便说一下,你的代码非常混乱。While循环不应用于此类操作(除非您尝试获取
退出
离开
的输入)


您也可以去掉最后一个while循环。

home()
函数中,
break
所做的就是打破循环。因此,当这种情况发生时,它将回到本世纪初

while finished == 0:
并将继续重复该输入

您还必须在
home()
(和
club()
)之后提供
break


顺便说一下,你的代码非常混乱。While循环不应用于此类操作(除非您尝试获取
退出
离开
的输入)


您也可以去掉最后的while循环。

我认为您需要将
当前\u房间
作为一个全局变量。因为
home()
中的变量
current\u room
while finished
中的变量具有不同的范围

我想,下面是你想要达到的目标。查找python变量作用域

chained = 1
finished = 0
current_room = 'home'

# home() and club() act as rooms in the game
def home():
    global chained
    # without the following line current_room has local scope and updating its value will not be
    # reflected in the while at the end
    global current_room 
    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
while finished == 0:
    if current_room == 'home':
        home()
    if current_room == 'club':
        club()

我认为您需要将
current\u room
作为一个全局变量。因为
home()
中的变量
current\u room
while finished
中的变量具有不同的范围

我想,下面是你想要达到的目标。查找python变量作用域

chained = 1
finished = 0
current_room = 'home'

# home() and club() act as rooms in the game
def home():
    global chained
    # without the following line current_room has local scope and updating its value will not be
    # reflected in the while at the end
    global current_room 
    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
while finished == 0:
    if current_room == 'home':
        home()
    if current_room == 'club':
        club()

虽然我从未真正理解代码的含义,但这里的解决方案是有效的。您还没有说明变量是全局变量还是局部变量,以及在可以使用简单的if-else语句时为什么要使用循环

chained = 0
finished = 0

# home() and club() act as rooms in the game
def home():
    global chained,current_room
    if chained == 1:
        # there's a way to unchain yourself that works
        chained = 0
    if 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'
            club()

# Not sure if finished is a local or global variable
def club():
    global finished,current_room
    print "this is messy code!!"
# These sort of move you around the rooms.
# current_room keeps track of what room you're in
    current_room = 'home'
    if finished == 0:
        if current_room == 'home':
            home()
        if current_room == 'club':
            club()
home()

虽然我从未真正理解代码的含义,但这里的解决方案是有效的。您还没有说明变量是全局变量还是局部变量,以及在可以使用简单的if-else语句时为什么要使用循环

chained = 0
finished = 0

# home() and club() act as rooms in the game
def home():
    global chained,current_room
    if chained == 1:
        # there's a way to unchain yourself that works
        chained = 0
    if 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'
            club()

# Not sure if finished is a local or global variable
def club():
    global finished,current_room
    print "this is messy code!!"
# These sort of move you around the rooms.
# current_room keeps track of what room you're in
    current_room = 'home'
    if finished == 0:
        if current_room == 'home':
            home()
        if current_room == 'club':
            club()
home()

现在,如果当前房间==“club”,这个错误行会做什么:在
home()
中的club()
当前房间
与主范围中的
当前房间
是不同的变量。您正在设置特定于函数的值,但它从不更新顶级作用域中具有相同名称的另一个变量。@Haidro“finished”是一个使其保持循环以移动房间的变量。基本上,每次你改变房间,你改变房间变量,停止函数,然后它检查房间变量,然后启动该房间的函数。编辑了主要帖子。现在,如果当前会议室=='club',这个错误行会做什么:
home()中的club()
current会议室
与主范围中的
current会议室
是不同的变量。您正在设置特定于函数的值,但它从不更新顶级作用域中具有相同名称的另一个变量。@Haidro“finished”是一个使其保持循环以移动房间的变量。基本上,每次你改变房间,你改变房间变量,停止函数,然后它检查房间变量,然后启动该房间的函数。编辑了主要帖子。我可能会用
.room
属性和
.home()
.club()
方法制作一个Game()类或类似类。是的,我也会这么做。但是这里给出的代码很混乱,他不知道python变量scope。我可能会用
.room
属性和
.home()
.club()
方法创建一个Game()类或类似类。是的,我也会这么做。但是这里给出的代码很混乱,他不知道python变量scope,我的while循环中的哪些不应该用于“this?”之类的事情?前两个while循环的目的是,如果输入错误,程序不会结束,我仍然需要添加。第二个是确保它不断检查当前的_room变量,我认为,如果我中断home()循环,它应该这样做。另外,在家里()和俱乐部()之后放休息时间只是结束了节目。我认为这不是他想要的。Variable current_room and chained存在局部范围与全局范围的问题。我的while循环中的哪一个不应该用于“this?”之类的事情?前两个while循环的要点是,如果输入错误,程序不会结束,我仍然需要添加。第二个是确保它不断检查当前的_room变量,我认为,如果我中断home()循环,它应该这样做。另外,在家里()和俱乐部()之后放休息时间只是结束了节目。我认为这不是他想要的。变量current_room和chained存在局部和全局范围问题。