function return None after execution of a function

Question:

I have this code to split a DNA strand into groups of 3. Everything in the result is intended except for that last "None"

def codons(x):
    for i in range(0, len(x), 3):
        result = print(x[i:i + 3])
    return result

When using with

print(codons('ATGCTCAAGTAGR'))

Returns:

ATG
CTC
AAG
TAG
R
None
Asked By: Rose Hoang

||

Answers:

Everything is right, just get rid of the return and the print when calling the function.

def codons(x):
    for i in range(0, len(x), 3):
        print(x[i:i + 3])
codons('ATGCTCAAGTAGR')
Answered By: Thavas Antonio

You can save the result in a list

 def codons(x):
        result=[]
        for i in range(0, len(x), 3):
            result.append(x[i:i + 3])
        return result
Answered By: Alan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.