Python Forum

Full Version: What exactly is wrong with my list.append function?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
                                        
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.
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.