I do not understand the error None in this function

Asked

Viewed 87 times

0

I cannot understand why in the end this function does not return a string. The error is in fptr.write(result + '\n'), as if the function returns an object of type None.

def dnaComplement(s):
    complement = []
    complement0 = reversed(s)
    for character in complement0:
        if character == 'G':
            complement.append('C')
        elif character == 'C':
            complement.append('G')
        elif character == 'A':
            complement.append('T')
        elif character == 'T':
            complement.append('A')
    final = ''.join(complement)
    print(final)

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = str(dnaComplement(s))

    fptr.write(result + '\n')

    fptr.close()

2 answers

0


The function failed to return a value. In the end it should have a return final:

def dnaComplement(s):
    complement = []
    complement0 = reversed(s)
    for character in complement0:
        if character == 'G':
            complement.append('C')
        elif character == 'C':
            complement.append('G')
        elif character == 'A':
            complement.append('T')
        elif character == 'T':
            complement.append('A')
    final = ''.join(complement)
    print(final)
    return final # <-- aqui

When there’s no return, the function returns None¹ (see more details on documentation). Placing a return value, it behaves as you expect, returning the string.


Or even, if you don’t want to print and you don’t want to use the variable final for nothing else, nor need it, can directly return the result of join:

def dnaComplement(s):
    complement = []
    complement0 = reversed(s)
    for character in complement0:
        if character == 'G':
            complement.append('C')
        elif character == 'C':
            complement.append('G')
        elif character == 'A':
            complement.append('T')
        elif character == 'T':
            complement.append('A')
    return ''.join(complement)

And as the return of join is already a string, no need to use str(dnaComplement(s)), just call the function directly you will already have a string:

s = input()
result = dnaComplement(s) # retorno da função é uma string, não precisa de str()
# etc...

Not directly related, but you can simplify the function using a dictionary containing the mappings between characters:

def dnaComplement(s):
    comps = { 'G': 'C', 'C': 'G', 'A': 'T', 'T': 'A' }
    complement = []
    for character in reversed(s):
        complement.append(comps[character])
    return ''.join(complement)

And you can still use one Generator Expression (much more succinct and pythonic), there is no need to generate the list complement:

def dnaComplement(s):
    comps = { 'G': 'C', 'C': 'G', 'A': 'T', 'T': 'A' }
    return ''.join(comps[character] for character in reversed(s))

1: the exception to this rule is when the function has a yield instead of return, then she becomes a Generator.

0

just like the friend said up there, you need to return a function.

another way to "force the string" with : return ''.join(complement)

you can return only:return str(complement)

Browser other questions tagged

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