Home > other >  For loop, if statement and Zip python
For loop, if statement and Zip python

Time:08-09

enter image description here

It shows unexpected result. The results are shown just for the first element of the lists.

CodePudding user response:

As you are using return It will only check for the first elements.

A return statement is used to end the execution of the function, Replace this with print() will loop over all the elements.

CodePudding user response:

I think your function quality_check() is returning after the first iteration of your for loop. Try:

a= ["Kiran", "Narut","Sasue"]
b= ["Kiran", "Naruto","Sasuke"]

def quality_check(x,y):
  for i,j in zip(x,y):
    if i != j:
      return "Wrong names"
  return "Good to go"

quality_check(a,b)

CodePudding user response:

You should use yield instead of return

a= ["Kiran", "Narut","Sasue"]
b= ["Kiran", "Naruto","Sasuke"]

def quality_check(x,y):
    for i,j in zip(x,y):
        if i == j:
            yield "good to go"
        else:
            yield "wrong"
for element in quality_check(a, b):
    print(element)
    

this will return required results every time you yield from function it returns a iterable object

  • Related