0
這是一個學校項目,我對一段代碼略有麻煩。 這是一個背景: 我正在做一個hang子手遊戲,它工作正常,但我試圖讓循環減少一個整數只有當它上面的if語句是假的。這似乎是一個問題,因爲它減少了整數的方式太多,例如,而不是10到9,它將其從10遞減到4. 任何幫助表示讚賞。 下面是代碼:Interger在Python for循環中遞減得太多
# This will run when count is above 0 (the length of the word.)
while guessesLeft > 0:
# Calls letterGuessQuery from the guessing module - gives the user the ability to guess a letter.
guessing.letterGuessQuery(guessesLeft,guessedLetters,coveredWord)
# Calls letterGuess from the guessing module.
guessing.letterGuess(guessedLetters)
# Gives the lenght of the randomWord - I use it as an index - I store in it i.
for i in range(len(randomWord)):
# Loops through randomWord and stores it in x
for x in randomWord[i]:
# Loops through the guessedLetters.
for z in guessedLetters:
# If the guessed letter is equal to the letter in random word.
if x == z:
# Modifies covered word to show the correct letter.
coveredWord[i] = x
else:
guessesLeft -=1
想想'guessesLeft - = 1'語句運行的次數。 – user2357112
在'guessesLeft - = 1'之後加上'print(「guessesLeft:」,guessesLeft)',你會看到它被執行了多少次。 – furas
哦......我明白了。任何想法如何我可以解決它而不改變整個代碼? – naf