1
votes

I am doing a problem in which I have created various function for the scrabble game. First, I want to ask the user to enter either n or r or e to begin a new game / replay the last hand / end the game.

Once the game has started, I want to ask the user to enter either u or c to have the user or computer play.

I am getting stuck in the last part of the problem.

My code:

hand = None
while True:
    choice = input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ').lower()
    if choice == 'e':
        break
    
    elif choice=='n':
        Your_choice = input('Enter u to play yourself or c to let the computer play: ')
        
        if Your_choice == 'u':
                hand = dealHand(HAND_SIZE)
                playHand(hand, wordList, HAND_SIZE)
        elif Your_choice == 'c':
                hand = dealHand(HAND_SIZE)
                compPlayHand(hand, wordList,HAND_SIZE)
        else:
                print('Invalid command.')
                
    elif choice == 'r':
        if hand == None:
            print('You have not played a hand yet. Please play a new hand first!')
        else:
            Your_choice = input('Enter u to play yourself or c to let the computer play: ')
            
            if Your_choice == 'u':
                if hand != None:
                    playHand(hand.copy(), wordList, HAND_SIZE)
                else:
                    print('You have not played a hand yet. Please play a new hand first!')
            elif Your_choice == 'c':
                if hand != None:
                    compPlayHand(hand.copy(), wordList, HAND_SIZE)
                else:
                    print('You have not played a hand yet. Please play a new hand first!')
            else:
                print('Invalid command.')
    else:
            print('Invalid command.')

If the choice of the inside loop is not u or c, it should repeatedly notify and ask until u or c is the input. But it come out of that loop after first instance.

Ideal Output:

Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter u to have yourself play, c to have the computer play: x
Invalid command.

Enter u to have yourself play, c to have the computer play: y
Invalid command.

Enter u to have yourself play, c to have the computer play: z
Invalid command.

Enter u to have yourself play, c to have the computer play: k
Invalid command.

My Output:

Enter n to deal a new hand, r to replay the last hand, or e to end game: n

Enter u to play yourself or c to let the computer play: x

Invalid command.

Enter n to deal a new hand, r to replay the last hand, or e to end game: y

Invalid command.

The problem is, when the user enters an invalid command at the second level, my code starts asking the question for the first level.

1
Drawing a flowchart of what you're trying to accomplish before you write the code helps you draw a mental picture of what your code needs to look like. Also, How to debug small programs. | What is a debugger and how can it help me diagnose problems?Pranav Hosangadi

1 Answers

1
votes

What you need is the break statement in python, it stops the innermost loop, and with that you can do something like:

while:
    Your_choice = input('Enter u to play yourself or c to let the computer play: ')
    if Your_choice in ["u", "c"]:
        # Do stuff
        break
    else:
        print("Incorrect option")

More info in the docs: https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops