0

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
  • 2
    What do you expect the function to do/return? – norie May 29 '21 at 21:20
  • This is why I never teach people about `print` too early. If the console is the only output device used in teaching, people tend to miss the difference between printing something and returning something, because ultimately all results are printed anyway. – Alexander May 29 '21 at 21:23
  • Does this answer your question? [How is returning the output of a function different from printing it?](https://stackoverflow.com/questions/750136/how-is-returning-the-output-of-a-function-different-from-printing-it) – mkrieger1 May 29 '21 at 21:51

2 Answers2

0

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
Alan
  • 24
  • 2
  • 2
-1

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')
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44