samedi 21 août 2021

Clarfication regarding the IF - Else ladder

I have written a function in which i have used a if else ladder and i have written it in two versions and in the first version there is no error but in the second version i am facing an error although both versions are performing the same tasks(It is done in python)

VERSION ONE OF THE CODE(NO ERROR)

def caught_speeding(speed,is_birthday):
if(speed>60 and speed<=80):
    if(is_birthday==True and speed<=65):
        print('no ticket')
    else:
        print('small ticket')
elif(speed>80):
    if(is_birthday==True and speed<=85):
        print('no ticket')
    else:
        print('big ticket')
else:
    print('no ticket')

VERSION TWO OF THE CODE(ERROR)

def caught_speeding(speed,is_birthday):
if(speed>60 and speed<=80):
    if(is_birthday==True and speed<=65):
        print('no ticket')
    else:
        print('small ticket')
else:
    if(speed>80):
        if(is_birthday==True and speed<=85):
            print('no ticket')
        else:
            print('big ticket')
else:
    print('no ticket')

The error that gets printed upon executing of versoin two of the code is:

File "<ipython-input-45-0ed6a5391a7f>", line 13
else:
^
SyntaxError: invalid syntax

I need clarifiaction on the "elif" part

Aucun commentaire:

Enregistrer un commentaire