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