我試圖創建一個遊戲,其中兩個用戶輸入字母,直到他們猜出祕密詞,但由於某種原因,我的列表沒有正確更新。可以說這個祕密詞是TOYS,如果用戶猜測它會顯示T _ _ _,然後用戶輸入R,所以它切換到第二個循環,用戶輸入S它表示S不在單詞中並且轉到第一個用戶。 這是一個猜謎遊戲,在兩個玩家之間發生變化,當它變成第二個玩家時,由於某種原因它不起作用。不知道爲什麼。函數secondLoop具有相同的組件與額外的行「#」Python兩個循環
回答
在你的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)
不,我不是說,當我把這些語句放到我的函數中時,就是我放入註釋的整個代碼,並且該函數不會調用它自己,這只是一條主線而已,調用函數 –
您所做的編輯仍然不起作用,我不知道爲什麼調用該函數與main相同。如果它只是一個玩家,那麼代碼完美地工作,但是一旦我打電話給第二個函數,它就會混亂起來 –
您能否提供堆棧跟蹤/錯誤消息的副本? – Genome
良好的程序結構,使生活變得更輕鬆:
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()
- 1. Python:兩個嵌套循環
- 2. Python的組合兩個for循環
- 3. Python一行循環兩個函數
- 4. ReactJS - 兩個循環?
- 5. 兩個While循環
- 6. Bash:while循環中的兩個循環
- 7. Foreach循環循環兩次
- 8. 兩個循環,排除第一循環後在第二循環
- 9. 兩個條件while循環
- 10. 兩個循環混合
- 11. 如何兩個for循環
- 12. 對兩個foreach循環
- 13. 與兩個循環交互
- 14. PHP循環兩個數組
- 15. 用兩個循環打印
- 16. jquery .each使兩個循環
- 17. 兩個不同的循環
- 18. jQuery每個循環兩次
- 19. 兩個循環在php
- 20. 組合兩個foreach循環
- 21. 兩個畫面循環
- 22. SDL:兩個事件循環?
- 23. Django ManyToMany兩個循環
- 24. 兩個變量的循環
- 25. Java:兩個'儘管'循環
- 26. 總計1,2,3 ...兩個循環
- 27. PHP:循環兩個陣列
- 28. 兩個循環同時?
- 29. 循環python中的循環
- 30. Python - 使用正則表達式循環兩個文件的循環
無論是張貼或您的壓痕超出當你沒有正確格式化你的代碼重擊。 –
爲什麼在第一個while循環之後沒有冒號? –
@ I.K。是更好的 –