Python:嵌套循环&继续循环

时间:2015-02-15 18:26:34

标签: python while-loop nested

我需要询问用户是否想要玩游戏,如果答案是y,那么程序会询问您想玩什么游戏,如果用户输入n甚至进入他们将被再次询问,直到他们进入y

现在我有了while循环接受任何答案。任何人都能看到我错过或做错了什么?

playAGame = 'y'
while playAGame == 'y':
    playAGame = raw_input('Would you like to play a game? (y/n)? ')
    doAnother = 'y'
    while doAnother == 'y':
            print 'Chess'
            print 'Tic Tac Toe'
            print 'Tetris'
            print 'Count'
            print
            print 'Global Thermonuclear War'
            game = raw_input('Pick a game: ').lower()
            if game == 'chess':
                    webbrowser.open_new("http://www.pygame.org/tags/chess")
                    break
            elif game == 'tic tac toe':
                        webbrowser.open_new("http://www.pygame.org/tags/tictactoe")
                        break
            elif game == 'tetris':
                        webbrowser.open_new("http://www.pygame.org/project-Clone+of+Tetris-2125-.html")
                        break
            elif game == 'thermonuclear':

                        print "\nWouldn't you perfer a good game of chess?\n"
                        break
            elif game == 'count':
                        maxNumber = input('How High? ')
                        for maxNumber in range(maxNumber):
                                print maxNumber
                                break
            else:
                    print '\nI did not understand that!\n'
                    break
    # Ask user if they want to do another
    doAnother = raw_input('Do another (y/n)? ')
    print '\nThanks for playing!'
    print '\nGood Bye!'

1 个答案:

答案 0 :(得分:1)

您的代码中的一个问题是此行doAnother = raw_input('Do another (y/n)? ')应缩进,以便它位于内部while循环中。另一个问题是,您的doAnotherplayAGame变量似乎具有相同的目的。您应该重新构建代码以删除其中一个代码。