为什么我的循环只有在代码几乎相同时才能正常工作?

问题描述 投票:0回答:1

这是我的Python代码的一部分,它主要按照我想要的方式工作,除了当猜测的字母错误时,它会错误地循环。我希望它循环回到要求一个字母,但它循环回到要求一个单词。如果猜测的字母正确,它将按预期循环。我该如何解决这个问题?

# start of game
letterTrue = input("Do you want to play?")

# variables
triesLeft = 1
guessList = []
triesRemaining = 3

# start guessing
while letterTrue == "yes" and triesLeft == True :
    word = input("\nEnter a word to start the game.")
    wordList = list(word)

# letter1 input
    letter1 = input("\nEnter a letter.") 

# letter 1 correct
    if letter1 in wordList :
        print("The letter", (letter1), "is in the word.")
        triesRemaining -= triesLeft
        print (triesRemaining, "tries left.")
        guessList.append(letter1)

# letter 1 wrong
    elif letter1 not in wordList :
        letterTrue == False
        (print("The letter", (letter1), "is not in the word."))
        triesRemaining -= triesLeft
        print (triesRemaining, "tries left.")

python loops while-loop
1个回答
0
投票

我相信这会做你想做的事:

# start of game
letterTrue = input("Do you want to play?")

# variables
triesLeft = 1
guessList = []
triesRemaining = 3

word = input("\nEnter a word to start the game.")
wordList = list(word)

# start guessing
while letterTrue == "yes" and triesLeft == True :

# letter1 input
    letter1 = input("\nEnter a letter.") 

# letter 1 correct
    if letter1 in wordList :
        print("The letter", (letter1), "is in the word.")
        guessList.append(letter1)

# letter 1 wrong
    elif letter1 not in wordList :
        letterTrue == False
        (print("The letter", (letter1), "is not in the word."))
        
    triesRemaining -= triesLeft
    print (triesRemaining, "tries left.")
© www.soinside.com 2019 - 2024. All rights reserved.