problem with None

Asked

Viewed 40 times

0

I’m doing an exercise here and the desired output would be Yes in this case but it’s returning me None. Could anyone help me? vlw

def alphabet (s,t,k):


    for i in range(0,len(s)):
        if s[i] != t[i] or s == 0: #o i indica o numero de chars que o s e o t tem em comum
            prefix = i
            break
        
            e = len(s) - prefix - len(t) - prefix

            if len(s) + len (t) < k:
                return 'Yes'
            elif e <= k and (e % 2) == (k % 2):
                return 'Yes'
            else:
                return 'No'






print (alphabet ('qwerasdf', 'qwerbsdf', 6))
  • Every function that does not have a Return explicit, automatically assumes return None. In your case, not going into the first if of the function, the return will be None

1 answer

0

The problem occurs because the function has no return to the print, when the code hits the break, the solution would be to add a return in the function, for example:

def alphabet (s,t,k):

    for i in range(0,len(s)):
        if s[i] != t[i] or s == 0: #o i indica o numero de chars que o s e o t tem em comum
            prefix = i
            break

            e = len(s) - prefix - len(t) - prefix
    
            if len(s) + len (t) < k:
                return 'Yes'
            elif e <= k and (e % 2) == (k % 2):
                return 'Yes'
            else:
                return 'No'

    return 'No | Yes'

print (alphabet ('qwerasdf', 'qwerbsdf', 6))

In that case you passed, would return 'No | Yes'.

Obs: Another detail is in relation to s == 0 here you are just comparing the content of the list with zero, this is exactly what you want to do?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.