为什么这个if语句没有执行?

时间:2017-01-11 17:11:35

标签: python python-3.x if-statement

我正在尝试猜词游戏。一切都有效,除了最后一个if语句部分。如果用户正确猜出了名称,它就不会打印出来!#34;赢了!"。但是,如果没有,则执行else语句,它会打印" Lost!"。这是为什么?我多次检查过,但我发现没有任何错误。

注意:我使用Python 3.6

    import random

def get_random_word():
    words = ["Ronnie", "Deskty", "Lorrie"]
    word = words[random.randint(0, len(words)-1)]
    return word

def show_word(word):
    for character in word:
        print(character, " ", end="")
    print("")

def get_guess():
    print("Enter a letter: ")
    return input()

def process_letter(letter, secret_word, blanked_word):
    result = False

    for i in range(0, len(secret_word)):
        if secret_word[i] == letter:
            result = True
            blanked_word[i] = letter

    return result

def print_strikes(number_of_strikes):
    for i in range(0, number_of_strikes):
        print("X ", end="")
    print("")

def play_word_game():
    strikes = 0
    max_strikes = 3
    playing = True

    word = get_random_word()
    blanked_word = list("_" * len(word))

    while playing:
        show_word(blanked_word)
        letter = get_guess()
        found = process_letter(letter, word, blanked_word)

        if not found:
            strikes += 1
            print_strikes(strikes)

        if strikes >= max_strikes:
            playing = False

        if not "_" in blanked_word:
            playıng = False


    if strikes >= max_strikes:
        print("Lost")
    else:
        print("Won")




print("Game started")
play_word_game()
print("Game over")

1 个答案:

答案 0 :(得分:8)

看看这段代码:

    if not "_" in blanked_word:
        playıng = False

您有ıU+0131 LATIN SMALL LETTER DOTLESS I)代替i。 Python将很乐意为您创建变量playıng(与其他必须声明变量的语言不同)。因此,playing永远不会在获胜案例中更新为False,并且函数永远不会退出。

正如Martijn在评论中指出的那样,通过发现错误拼写的变量和其他可能的错误,一个短信可能会有所帮助。考虑在编辑器或IDE中使用一个。两个流行的短信是PylintFlake8