while循环后值不重置

时间:2015-12-05 01:11:41

标签: python loops while-loop

我正在研究MOOC的问题,不得不放弃并查看解决方案。但我不确定这个解决方案的某个特定部分。这是代码:

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."

手的价值怎么可能不会重置为新值?因此,例如,用户输入是" n"第一次,输入的第二次是' r'。那个手变量不应该没有价值吗?但实际上,手的价值保持不变。回来时,while循环中的所有内容都没有重置吗?或者我错了吗?

0 个答案:

没有答案
相关问题