Python Forum
What exactly is wrong with my list.append function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What exactly is wrong with my list.append function?
#1
my question here
Hello. I have been practicing on my Python skills by making a little program translating DRA/RNA codons. However, while everything else works correctly thus far, it seems that my lis.append function doesn't work properly, and doesn't actually add items to my list. Can someone spot what is going on?
def analyser():
        bases=("A","T","U","G","C")
        type=0 #0=Undefined, 1=DNA, 2=RNA, 3=Hybrid DNA/RNA
        data=input("Please insert your DNA or RNA strand here:")
        print("Checking for errors...")
        #The following segment is incomplete, as it assumed that all codons are translated toaminoacids-regardless of start/end codons. Updates should change this segment to allow a more realistic spectrum of strands.
        if len(data)%3!=0:
                print("Incompatible number of bases. Please check your input and try again.")
                analyser()
        for i in range(1,len(data)):
                if data[i] not in bases:
                        print("Incorrect base input found at position",i+1,"of the strand. Please correct your input and try again.")
                        analyser()
        if "T" in data and "U" not in data:
                print("Strand classified as a DNA strand.")
                type=1
        if "U" in data and "T" not in data:
                print("Strand classified as an RNA strand.")
                type=2
        if "T" in data and "U" in data:
                print("Strand contains both U and T bases. The strand will be classified as a hybrid DNA/RNA strand, and translated accordingly.")
                type=3
        if "T" not in data and "U" not in data:
                print("Strand type not determined. The strand will be translated regardless.")
                type=0
        print("Translating strand...")
        amino=[]
        for i in range(1,len(data),3):
                if data[i+1]=="A":
                        if data[i+2]=="T" or data[i+2]=="U":
                                if data[i+3]=="G":
                                        amino.append("Methionine")
                                else:
                                        amino.append("Isoleucine")
                        if data[i+2]=="C":
                                amino.append("Threonine")
                        if data[i+2]=="A":
                                if data[i+3]=="A" or data[i+3]=="G":
                                        amino.append("Lysine")
                                else:
                                        amino.append("Asparagine")
                        if data[i+2]=="G":
                                if data[i+3]=="A" or data[i+3]=="G":
                                        amino.append("Arginine")
                                else:
                                        amino.append("Serine")
        #The rest of the aminoacids have to be added.
        print("The strand translates into a polypeptide chain with this aminoacid composition:")
        print(amino)
                                        
Reply
#2
It's working for me. I assume you are actually calling the analyser function at some point, that's not in your posted code, though. What input are you giving it that is producing no appends?

You realize Python is 0 indexed, right? Take line 29. One the first pass, i is going to be 1. So data[i + 1] is going to be data[2], which is the third character in the string.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Good catch. It must have stuck with me from previously, where I used i+1 to print the char positions correctly. (I was working on this late at night with no coffee, okay? Don't judge me XD)
Yep, that seems to be the problem. Thanks! Btw, the reason I don't call analyser() in the code is because I was testing it on IDLE, which for some reason required me to FIRST define it, THEN call it. Go figure.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 345 Mar-14-2024, 06:26 PM
Last Post: flash77
Question How to append integers from file to list? Milan 8 1,366 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,388 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  read a text file, find all integers, append to list oldtrafford 12 3,372 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 9,909 Jun-12-2022, 06:54 PM
Last Post: Mark17
  print function output wrong with strings. mposwal 5 3,046 Feb-12-2021, 09:04 AM
Last Post: DPaul
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,280 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  How to append to list a function output? rama27 5 6,648 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  Append list into list within a for loop rama27 2 2,313 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  Append only adding the same list again and again viraj1123 4 1,996 Jun-17-2020, 07:26 AM
Last Post: viraj1123

Forum Jump:

User Panel Messages

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