dimanche 21 juillet 2019

Why does the code executes differently once I introduce else-statement

I was doing exercises at CodingBat today and came across an interesting problem: This code runs wrong half times:

for i in range(len(nums)-1):
    if nums[i]==2 and nums[i+1] ==2:
      print (True)
    else: 
      print (False)

And this is completely correct:

for i in range(len(nums)-1):
    if nums[i]==2 and nums[i+1] ==2:
      print (True)

  print (False)

My question is why does the introduction of else statement changes the way code works

Aucun commentaire:

Enregistrer un commentaire