Home > Mobile >  Trying to go back one loop but not to main parent loop
Trying to go back one loop but not to main parent loop

Time:10-01

This game requires two inputs from the user: "user_choice" and "player_choice". I get the user_choice input first. The final issue I am running into is on the 2nd input (player_choice). If the user enters something invalid it is making them reselect user_choice, and I just want them to have to reselect player_choice.

def playGame(wordList):
       
    hand = []   
    while True:   
    # ask the user if they want to play
        user_choice = input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
        # if they enter 'e' end the game
        if (user_choice == 'e'):
            break
        elif (user_choice == 'r' and len(hand) == 0):
            print('You have not played a hand yet. Please play a new hand first!')
        # if they enter an invalid command send them back
        elif (user_choice != 'n' and user_choice != 'r'):
            print('Invalid command.')
        
        # if they enter n or r ask them who is going to play
        if (user_choice == 'n' or user_choice == 'r'):
            player_choice = input('Enter u to have yourself play, c to have the computer play: ')
            # if human is playing follow the steps from before
            if (player_choice == 'u'):
                if (user_choice == 'n'):
                    hand = dealHand(HAND_SIZE)
                    playHand(hand, wordList, HAND_SIZE)
                elif (user_choice == 'r' and len(hand) != 0):
                    playHand(hand, wordList, HAND_SIZE)
                else :
                    print('You have not played a hand yet. Please play a new hand first!')
                    
            # if computer is playing . . .
            elif (player_choice == 'c'):
                if (user_choice == 'n'):
                    hand = dealHand(HAND_SIZE)
                    compPlayHand(hand, wordList, HAND_SIZE)
                elif (user_choice == 'r' and len(hand) != 0):
                    compPlayHand(hand, wordList, HAND_SIZE)
                else:
                    print('You have not played a hand yet. Please play a new hand first!')
            else:
                print('Invalid command.')
                continue

'''

CodePudding user response:

Your continue statement on an invalid choice goes to the next iteration of the loop, where user_choice will be requested again.

            elif (player_choice == 'c'):
                if (user_choice == 'n'):
                    hand = dealHand(HAND_SIZE)
                    compPlayHand(hand, wordList, HAND_SIZE)
                elif (user_choice == 'r' and len(hand) != 0):
                    compPlayHand(hand, wordList, HAND_SIZE)
                else:
                    print('You have not played a hand yet. Please play a new hand first!')
            else:
                print('Invalid command.')
                continue # Will go to the start of the loop

Instead you can wrap the player_choice code into a loop that repeats whilst the input is invalid. This could look something like:

player_choice_valid = False
while not player_choice_valid:
   # As you have multiple valid cases but one invalid case,
   # by setting to True here we can save writing it for each valid case
   player_choice_valid = True
   player_choice = input("...")
   ...
   elif (player_choice == 'c'):
       ...
   else
       player_choice_valid = False # Input is not valid, set to False
  • Related