繁体   English   中英

如何打破多个while循环

[英]How to break multiple while loops

我正在制作一个程序,它是一个拿起棍子的游戏。 我仍然对整个事情的逻辑感到困惑。 我最大的问题是我有多个嵌套的 while 循环并想结束所有循环。 继承人我的代码。

x = 1

while x == 1:
   sticks = int(input('How many sticks are on the tables (10 to 100): '))


   if sticks not in range(10,101):
        print('Invalid.')
        continue
   while x == 1:
        print('There are',sticks,'sticks on the table.')
        print('Player 1')
        p1 = int(input('How many sticks do you want to remove?'))
        if p1 == sticks:
            print('Player one wins.')
            x == 2
            break
        elif p1 not in range(1,4):
            print('Invalid.')
            continue
        else:
            while x == 1:
                sticks -= p1
                print('There are',sticks,'sticks on the table.')
                print('Player 2.')
                p2 = int(input('How many sticks do you want to remove?'))
                if p2 == sticks:
                        print('Player two wins.')
                        x == 2 
                        break

                elif p2 not in range(1,4):
                    print('Invalid.')
                    continue
                else:
                    sticks -= p2

我的输出继续提示玩家 1 和 2 进行输入。

我希望程序在打印“Player _ wins”后结束。 任何帮助/提示将不胜感激! 或者甚至更简单的方法来编写程序。

我总是发现为多人回合制游戏构建状态机有很大帮助。 因为它提供了一种清晰而简单的方法来分解逻辑,避免在嵌套循环中使用大量的breakcontinue甚至goto

例如,这是一个有 4 个状态的状态机: 在此处输入图片说明

对于每个状态,都有一个处理函数,它会根据当前玩家、摇杆和用户输入决定下一个状态(甚至是它自己):

def initialize():
    global sticks, state
    n = int(input('How many sticks are on the tables (10 to 100): '))
    if n not in range(10, 101):
        print('Invalid. It should be between 10 ~ 100.')
    else:
        state = 'ask_player1'
        sticks = n


def ask_player1():
    global sticks, state
    print('There are', sticks, 'sticks on the table.')
    print('Player 1')
    n = int(input('How many sticks do you want to remove?'))
    if n not in range(1, 4):
        print('Invalid. It should be between 1 ~ 4')
    else:
        sticks -= n
        if sticks == 0:
            print('Player one wins.')
            state = 'end'
        else:
            state = 'ask_player2'


def ask_player2():
    global sticks, state
    print('There are', sticks, 'sticks on the table.')
    print('Player 2')
    n = int(input('How many sticks do you want to remove?'))
    if n not in range(1, 4):
        print('Invalid. It should be between 1 ~ 4')
    else:
        sticks -= n
        if sticks == 0:
            print('Player two wins.')
            state = 'end'
        else:
            state = 'ask_player1'


state_machine = {
    'initialize': initialize,
    'ask_player1': ask_player1,
    'ask_player2': ask_player2,
}

sticks = 0
state = 'initialize'
while state != 'end':
    state_machine[state]()

最好的办法可能是将代码移动到返回的函数中。 多次提出多断,都被否决了 您可以将整个代码放入一个在“Player _ wins”后返回的函数中,这样它就不会继续运行。 其他替代方法是使用在玩家获胜后设置的标志变量并引发随后处理的异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM