samedi 21 mars 2020

Why do I get a Syntax error when using Else and not Elif?

I'm just writing a simple if/elif/else statement and I keep getting a syntax error when using else but not elif. They are being used in the exact same place. Here are two examples:

fruit = 'banana'
    if 'e' in fruit:
        print('found it')
    elif 'b' in fruit:
        print('found it')

fruit = 'banana'
if 'e' in fruit:
    print('found it')
else 'b' in fruit:
    print('found it')

The elif code runs with no problems, whereas the code using else produces a syntax error:

line 4 else 'b' in fruit: ^ SyntaxError: invalid syntax

Aucun commentaire:

Enregistrer un commentaire