dimanche 27 mai 2018

If else return issue in the loop in python

I need to make expression as:

def foo():    
number = 0
for x in request:
   if x is y:
       number += 1

    return "x is not y"

return number

but even if x is y it gives "x is not y". If I use else like:

def foo():
number = 0
for x in request:
   if x is y:
       number += 1

   else:
        return "x is not y"

return number

But else return is against syntax rools. How to correct code?

Aucun commentaire:

Enregistrer un commentaire