I was working on a problem from a MOOC, and had to give up and look at the solution. But I am not sure about a particular part of this solution. Here is the code:
def playGame(wordList):
"""
Allow the user to play an arbitrary number of hands.
1) Asks the user to input 'n' or 'r' or 'e'.
* If the user inputs 'n', let the user play a new (random) hand.
* If the user inputs 'r', let the user play the last hand again.
* If the user inputs 'e', exit the game.
* If the user inputs anything else, tell them their input was invalid.
2) When done playing the hand, repeat from step 1
"""
turn = 0
user_input = ""
while user_input != 'e':
user_input = raw_input("Enter n to deal a new hand, r to replay the last hand, or e to end game: ")
if user_input == 'r' and turn ==0:
print "You have not played a hand yet. Please play a new hand first! \n"
elif user_input == 'r' and turn > 0:
playHand(hand, wordList, HAND_SIZE)
elif user_input == 'n':
hand = dealHand(HAND_SIZE)
playHand(hand, wordList, HAND_SIZE)
turn = 1
elif user_input !='e':
print "Invalid command."
How is it possible that the value of hand doesn't reset to a new value? So for example, user input is "n" the first time around, and second time around the input is 'r'. Shouldn't there be no value for the hand variable? But in reality the value for hand is staying the same. Doesn't everything in the while loop reset when coming back around? Or am I wrong?