1

我正在写一个可怕的基于文本的冒险,我不知道如何打破这个循环。我将尝试在此处发布相关内容。如果我的评论不能充分解释我想要做什么,请告诉我。

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

如果必须,我会发布我完全未删节的代码,但我不想这样做,因为实际的冒险并不完全......专业。

4

3 回答 3

4

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

while finished == 0:

并将继续重复该输入。

您还必须提供一个break之后home()(和club()):

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

顺便说一句,您的代码非常混乱。While 循环不应该用于这样的事情(除非你试图获得exitor的输入leave

您不妨摆脱最后的 while 循环。

于 2013-08-22T04:25:26.963 回答
0

我认为你需要做current_room一个全局变量。因为变量current_roominhome()和 inwhile 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()
于 2013-08-22T04:35:13.470 回答
0

虽然我从来没有真正理解过代码对这里有效的解决方案意味着什么。您还没有说明变量是全局变量还是局部变量,以及当可以使用简单的 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()
于 2013-08-22T04:42:35.117 回答