Python Forum
String substitution with dictionary - AttributeError: 'NoneType'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String substitution with dictionary - AttributeError: 'NoneType'
#1
Hi,

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

My code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:
1
AttributeError: 'NoneType' object has no attribute 'append'
What's the reason for this 'NoneType' object?

Regards,
Atalanttore
Reply
#2
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
1
rna_list = rna_list.append(dna_rna[dna])
to
1
rna_list.append(dna_rna[dna])
and it should work just fine :)
Reply
#3
Thanks for your explanation.Smile It solved this problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Substitution call keys Bobbee 15 2,607 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  AttributeError: 'NoneType' re.search philnyland 1 940 Jan-20-2024, 03:24 AM
Last Post: deanhystad
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 3,567 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 3,245 May-13-2022, 03:44 PM
Last Post: deanhystad
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 6,926 Jan-02-2022, 09:53 AM
Last Post: snippsat
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 5,026 Jan-01-2022, 04:16 PM
Last Post: MaartenRo
  AttributeError: module 'string' has no attribute 'uppercase' Anldra12 10 13,677 Apr-23-2021, 05:30 PM
Last Post: ibreeden
  Beautify dictionary without converting to string. sharoon 6 4,581 Apr-11-2021, 08:32 AM
Last Post: buran
  AttributeError: 'NoneType' object has no attribute 'next' loves 2 14,959 Dec-15-2020, 11:30 PM
Last Post: bowlofred
  extract a dictionary from a string berc 4 4,225 Jul-30-2020, 06:58 AM
Last Post: berc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020