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:
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
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
rna_list = rna_list.append(dna_rna[dna])
to
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
  AttributeError: 'NoneType' re.search philnyland 1 292 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 1,497 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 1,861 May-13-2022, 03:44 PM
Last Post: deanhystad
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 4,706 Jan-02-2022, 09:53 AM
Last Post: snippsat
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 2,681 Jan-01-2022, 04:16 PM
Last Post: MaartenRo
  AttributeError: module 'string' has no attribute 'uppercase' Anldra12 10 10,276 Apr-23-2021, 05:30 PM
Last Post: ibreeden
  Beautify dictionary without converting to string. sharoon 6 3,381 Apr-11-2021, 08:32 AM
Last Post: buran
  AttributeError: 'NoneType' object has no attribute 'next' loves 2 11,614 Dec-15-2020, 11:30 PM
Last Post: bowlofred
  extract a dictionary from a string berc 4 2,857 Jul-30-2020, 06:58 AM
Last Post: berc
  Need help with for loop and variable value substitution in a function rsurathu 2 2,395 Jul-21-2020, 06:47 AM
Last Post: rsurathu

Forum Jump:

User Panel Messages

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