2014-04-05 83 views
0

我試圖創建一個遊戲,其中兩個用戶輸入字母,直到他們猜出祕密詞,但由於某種原因,我的列表沒有正確更新。可以說這個祕密詞是TOYS,如果用戶猜測它會顯示T _ _ _,然後用戶輸入R,所以它切換到第二個循環,用戶輸入S它表示S不在單詞中並且轉到第一個用戶。 這是一個猜謎遊戲,在兩個玩家之間發生變化,當它變成第二個玩家時,由於某種原因它不起作用。不知道爲什麼。函數secondLoop具有相同的組件與額外的行「#」Python兩個循環

+0

無論是張貼或您的壓痕超出當你沒有正確格式化你的代碼重擊。 –

+0

爲什麼在第一個while循環之後沒有冒號? –

+0

@ I.K。是更好的 –

回答

0

在你的secondLoop函數中,它似乎永遠遞歸調用自己。這可能是導致你的錯誤的原因。我改變了遞歸自調用返回語句,它似乎工作正常。我在下面評論例如改變...

def secondLoop(enteredLetters,sentence,newSentence,lettersInWord): 
    print '--Player 2--' 
    number = len(sentence) 
    flag = True 
    while flag == True: 
     # changed input to raw_input 
     letter = raw_input("Enter letter: ") 

     if letter not in enteredLetters: 
      find = sentence.find(letter) 
      if find == -1: 
       print("Letter is not in word") 
       flag = False 
       # --- changed line below to return statement below it --- 
       #enteredLetters,newSentence,lettersInWord = secondLoop(enteredLetters,sentence,newSentence,lettersInWord) 
       return enteredLetters,newSentence,lettersInWord 
      else: 
       newSentence = "" 
       lettersInWord.append(letter) 
       for char in sentence: 
        if char in lettersInWord: 
         newSentence = newSentence + char 
        else: 
         newSentence = newSentence + "_ " 
       number = newSentence.count("_") 
       flag = True 
     elif letter in enteredLetters: 
      print ("The letter has been used") 
      flag = False 
      # --- changed line below to return statement below it --- 
      #enteredLetters,newSentence,lettersInWord = secondLoop(enteredLetters,sentence,newSentence,lettersInWord) 
      return enteredLetters,newSentence,lettersInWord 

     print (newSentence) 
     enteredLetters.append(letter)  

sentence = 'TOYS' 
enteredLetters = [] 
newSentence = '____' 
lettersInWord = [] 

number = len(sentence) 
while number != 0: 
    print '--Player 1--' 
    # changed input to raw_input 
    letter = raw_input("Enter letter: ") 

    if letter not in enteredLetters: 
     find = sentence.find(letter) 
     if find == -1: 
      print("Letter is not in word") 
      enteredLetters,newSentence,lettersInWord = secondLoop(enteredLetters,sentence,newSentence,lettersInWord) 
     else: 
      newSentence = "" 
      lettersInWord.append(letter) 
      for char in sentence: 
       if char in lettersInWord: 
        newSentence = newSentence + char 
       else: 
        newSentence = newSentence + "_ " 
      number = newSentence.count("_") 
    elif letter in enteredLetters: 
     print ("The letter has been used") 
     enteredLetters,newSentence,lettersInWord = secondLoop(enteredLetters,sentence,newSentence,lettersInWord) 
    print (newSentence) 
    enteredLetters.append(letter)  
+0

不,我不是說,當我把這些語句放到我的函數中時,就是我放入註釋的整個代碼,並且該函數不會調用它自己,這只是一條主線而已,調用函數 –

+0

您所做的編輯仍然不起作用,我不知道爲什麼調用該函數與main相同。如果它只是一個玩家,那麼代碼完美地工作,但是一旦我打電話給第二個函數,它就會混亂起來 –

+0

您能否提供堆棧跟蹤/錯誤消息的副本? – Genome

0

良好的程序結構,使生活變得更輕鬆:

from itertools import cycle 
import string 

UNKNOWN  = "_" 
ALWAYS_SEEN = set(" " + string.punctuation) 

def get_letter(prompt): 
    while True: 
     ch = input(prompt).strip().upper() 
     if len(ch) == 1: 
      return ch 

def seen_word(word, guessed): 
    return ''.join(ch if ch in guessed or ch in ALWAYS_SEEN else UNKNOWN for ch in word) 

def play_hangman(word, players): 
    word = word.strip().upper() 
    guessed = set() 
    seen = seen_word(word, guessed) 

    for current_player in cycle(players): 
     while True: 
      print(seen) 
      ch = get_letter("{}: guess a letter: ".format(current_player)) 

      if ch in guessed: 
       print("That letter has already been guessed!") 
       break 
      else: 
       guessed.add(ch) 

      if ch in word: 
       print("That's the way to do it!") 
       seen = seen_word(word, guessed) 
      else: 
       print("Sorry, that letter is not in the word.") 
       break 

      if seen == word: 
       print("Hurray! {} won!".format(current_player)) 
       return current_player 

def main(): 
    word = "TOYS" 
    players = ["Player 1", "Player 2"] 
    play_hangman(word, players) 

if __name__=="__main__": 
    main()