0

I need to ask the user if they would like to play a game, if the answer is y then the program will then ask you what game you want to play, if the user enters n or even enters they will be asked again till they enter y.

Right now I have the while loop accepting any answer. Anyone able to see what I'm missing or doing wrong?

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!'
4
  • What's the point of playAGame = 'y'; if playAGame == 'y':?! Also, note that you only ask the user if they want to play again if they played count. Commented Feb 15, 2015 at 18:36
  • if is suppose to be a while, but I'm still stuck... Commented Feb 15, 2015 at 18:50
  • Please cut this down to a minimal example - most of the code is irrelevant. Also, clarify the behaviour you're expecting, and what happens instead. Commented Feb 15, 2015 at 18:52
  • Rather than using playAGame as a character, use a bool. For instance, name it isPlaying = True at the start and set it to false when you expect to quit. Or just use a break statement to exit the loop. Commented Feb 15, 2015 at 18:52

1 Answer 1

1

One of the problems in your code is that this line doAnother = raw_input('Do another (y/n)? ') should be indented so that it is in the inner while loop. Another problem is that your doAnother and playAGame variables seem to have the same purpose. You should restructure your code to remove one of them.

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't it already in the inner loop? Scratches head I need to have two loops.
@Jake No, it's in the outer loop. It is at the same indentation as the inner loop while statement, it needs to be indented further to be part of body of the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.