Elif无效的语法python

时间:2014-04-21 14:00:46

标签: python python-3.x

我的elif语句中的语法无效,为什么会出现代码:

while correct_turns != len(word):
        if correct_turns == len(word):
            finish()
        guess = input("Please guess a letter from above: ")
        elif guess in word and not LettersUnused:
            used_letter(guess,display,LettersUnused)
        elif guess in word and LettersUnused:
            correct(guess,display,correct_turns,LettersUnused)
        elif guess in LettersUnused and not word:
            incorrect(guess,display,LettersUnused)
        elif guess not in LettersUnused:
            used_letter(guess,display,LettersUnused)
        elif len(guess)>1:
            guess_word(guess)

2 个答案:

答案 0 :(得分:2)

根据您的缩进,您的第一个elif应该只是if。无论如何elif与第一个if无关。

答案 1 :(得分:2)

您不需要初始if,因为您的while循环条件会阻止循环输入。

你无法继续'继续'一个if块,中间有另一条线。将第一个elif更改为if以启动新块;您也可以删除其他if语句,因为它永远不会执行。

while correct_turns != len(word):
    guess = input("Please guess a letter from above: ")
    if guess in word and not LettersUnused:
        used_letter(guess,display,LettersUnused)
    elif guess in word and LettersUnused:
        correct(guess,display,correct_turns,LettersUnused)
    elif guess in LettersUnused and not word:
        incorrect(guess,display,LettersUnused)
    elif guess not in LettersUnused:
        used_letter(guess,display,LettersUnused)
    elif len(guess)>1:
        guess_word(guess)