samedi 15 août 2020

why use "else"" unnecessarily?

ìn the given below code ... if x is there in list then we will return or we will automatically fall out after the loop is completed . We will fall on print(x,"is not in list",end='') then return False .Why do we need 'else' then ??

def search(self,x):
        p=self.start
        position = 1
        while p is not None :
            if p.info==x:
                print(x,"is at position",position)
                return True
            p=p.link
            position +=1
        else:
            print(x,"is not in the list",end='')
            return False

Aucun commentaire:

Enregistrer un commentaire