【问题标题】:Can't break out of a while loop无法跳出while循环
【发布时间】: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_room in home() 是与主作用域中的 current_room 不同的变量。您正在设置该函数特定的值,但它永远不会更新顶级范围内具有相同名称的其他变量。
  • @Haidro 'finished' 是一个变量,它可以保持循环以移动房间。基本上,每次你改变房间时,你都会改变房间变量,停止函数,然后检查房间变量,然后启动那个房间的函数。编辑了主帖。

标签: python while-loop


【解决方案1】:

break 所做的是打破home() 函数中的循环。所以当它中断时,它会回到开头

while finished == 0:

并将继续重复该输入。

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

while finished == 0:
    if current_room == 'home':
        home()
        break
    if current_room == 'club':
        club()
        break

顺便说一句,您的代码非常混乱。 While 循环不应该用于这样的事情(除非您尝试获取exitleave 的输入)

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

【讨论】:

  • 我的哪些 while 循环不应该用于“this”之类的事情?前两个while循环的要点是如果输入错误,程序不会结束,我仍然需要添加。第二个是确保它不断检查 current_room 变量,如果我打破 home() 循环,它应该这样做,我想。此外,在 home() 和 club() 之后放置 break 只会结束程序。
  • 我不认为这是他想要的。变量 current_room 和 chained 存在局部和全局范围问题。
【解决方案2】:

虽然我从来没有真正理解代码的含义,但这里的解决方案是有效的。您还没有说明变量是全局变量还是局部变量,以及当可以使用简单的 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()

【讨论】:

    【解决方案3】:

    我认为您需要将current_room 设为全局变量。因为home()中的变量current_roomwhile 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()
    

    【讨论】:

    • 我可能会使用 .room 属性和 .home().club() 方法创建一个 Game() 类或类似的类。
    • 是的,我也是。但是这里给出的代码比较乱,他不知道python的变量作用域
    猜你喜欢
    • 2020-01-03
    • 2013-05-06
    • 2020-04-17
    • 2021-08-29
    • 2015-08-04
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多