String substitution with dictionary - AttributeError: 'NoneType' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: String substitution with dictionary - AttributeError: 'NoneType' (/thread-9884.html) |
String substitution with dictionary - AttributeError: 'NoneType' - Atalanttore - May-02-2018 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 functionUnfortunately, 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 RE: String substitution with dictionary - AttributeError: 'NoneType' - ThiefOfTime - May-02-2018 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 :) RE: String substitution with dictionary - AttributeError: 'NoneType' - Atalanttore - May-02-2018 Thanks for your explanation. It solved this problem. |