Не удается определить синтаксическую ошибку в моем цикле for

#python

#python

Вопрос:

Получаю синтаксические ошибки, не могу определить, почему

 elif num ==2:
    value = int(input("How many vowels does the word contain?"))
    if value == countVowels(item):
        score = score   1
    print("Correct!")
else:
        print("Incorrect! Correct answer is", countVowels(item))        

elif num ==3:
    value = int(input("How many consonants does the word contain?"))
    if value == (len(item) - countVowels(item)):
        score = score   1
    print("Correct!")
  

Получение недопустимого синтаксиса в elif for num==3 , но не могу определить, почему.

Комментарии:

1. извините за вопрос новичка, новичок здесь

2. У вас не может быть a elif после a else .

3. И есть ли у вас вообще if на первом месте?

Ответ №1:

     if value == countVowels(item):
        score = score   1
    print("Correct!")
else:
        print("Incorrect! Correct answer is", countVowels(item))
  

Эта часть выглядит так, как будто отступы все перепутаны.

Вы имели в виду:

     if value == countVowels(item):
        score = score   1
        print("Correct!")
    else:
        print("Incorrect! Correct answer is", countVowels(item))
  

?

Ответ №2:

Ваш elif не может появиться после else . Порядок должен быть таким:

 if <cond1>:

elif <cond2>:

elif <cond3>:

else:
  

if Требуется, чтобы быть первым. Тогда вы можете дополнительно иметь один или несколько elif s и необязательный else в конце.

Чтобы исправить свой код, вы можете поместить else после внутреннего if:

 elif num == 2:
    value = int(input("How many vowels does the word contain?"))
    if value == countVowels(item):
        score = score   1
        print("Correct!")
    else:
       print("Incorrect! Correct answer is", countVowels(item))        

elif num == 3:
    value = int(input("How many consonants does the word contain?"))
    if value == (len(item) - countVowels(item)):
        score = score   1
        print("Correct!")
    else:
        print("Incorrect! Correct answer is", countVowels(item))