1

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.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 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.](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) | [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/843953) – Pranav Hosangadi Oct 06 '20 at 17:39

1 Answers1

1

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

IamFr0ssT
  • 729
  • 5
  • 11