Python Forum

Full Version: String substitution with dictionary - AttributeError: 'NoneType'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm working on a Exercism programming exercise for transcribing DNA into RNA.

My code looks like this:
def dna_to_rna(dna_strand):

    dna_rna = {'G':'C',
               'C':'G',
               'T':'A',
               'A':'U'}

    dna_list = list(dna_strand)
    rna_list = []

    for dna in dna_list:
        rna_list = rna_list.append(dna_rna[dna])
    return rna_list

dna_to_rna('ATCGGTTA') # Test function
Unfortunately, this code produces following error and I have no clue why:
AttributeError: 'NoneType' object has no attribute 'append'
What's the reason for this 'NoneType' object?

Regards,
Atalanttore
The reason for this is that rna_list.append does not return a value. The default for a function returning nothing is None. Since None does not have a function append, you get this error. The function append already appends to the list, there is no return value needed. so just change the line
rna_list = rna_list.append(dna_rna[dna])
to
rna_list.append(dna_rna[dna])
and it should work just fine :)
Thanks for your explanation.Smile It solved this problem.