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.