lundi 8 novembre 2021

Python running elif block, when the if block is true

Why did the elif statement execute even though the if was already true

s=input("Enter the string: ")
c=3
while True:
    if s[0]=="a":
        s=s[2:]
    elif s[-1]=="b":
        s=s[:2]
    else:
        c+=1
        break
    print(s)
    print(c)

Output:

Enter the string: aaccbb
ccbb
3
cc
3

Aucun commentaire:

Enregistrer un commentaire