Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 What exactly is wrong with my list.append function?
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():
        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.")
        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.")
        if "T" in data and "U" not in data:
                print("Strand classified as a DNA strand.")
        if "U" in data and "T" not in data:
                print("Strand classified as an RNA strand.")
        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.")
        if "T" not in data and "U" not in data:
                print("Strand type not determined. The strand will be translated regardless.")
        print("Translating strand...")
        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":
                        if data[i+2]=="C":
                        if data[i+2]=="A":
                                if data[i+3]=="A" or data[i+3]=="G":
                        if data[i+2]=="G":
                                if data[i+3]=="A" or data[i+3]=="G":
        #The rest of the aminoacids have to be added.
        print("The strand translates into a polypeptide chain with this aminoacid composition:")
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 -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

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.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  python gives wrong string length and wrong character thienson30 2 157 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 127 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  List comprehensions-Wrong result RavCOder 4 120 Oct-08-2019, 10:16 AM
Last Post: RavCOder
  Iterate through a list of dictionary and append a new value. erina 1 262 May-16-2019, 09:55 AM
Last Post: perfringo
  Is there a way to append to a list WITHOUT modifying the parameter passed? arnavb 11 1,140 Sep-23-2018, 07:16 AM
Last Post: Skaperen
  lots of ['s and ]'s in key value list append hokie1999 6 734 Aug-28-2018, 06:36 PM
Last Post: buran
  Trying to append List/array based on condition Mr_Keystrokes 1 721 Mar-20-2018, 04:11 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)