0

I have a string like rna = "UACGAUGUUUCGGGAAUGCCUAAAUGUUCCGGCUGCUAA" and I want to iterate through the string and capture the different strings which start with 'AUG' and with 'UAA' or 'UAG' or 'UGA'.

This is the code I've written so far:

rna = "UACGAUGUUUCGGGAAUGCCUAAAUGUUCCGGCUGCUAA"      # start --> AUG; STOP --> UAA, UAG, UGA
hello = " "
n = 3
list = []
for i in range(0, len(rna), n):
    list.append(rna[i:i+n])

for i in list:
    if i == "AUG":
        hello += i
        i = i + 1
        if i != "UAA" or "UAG" or "UGA":
            hello += i

This is giving me a type error:- error

  • 2
    Please include the error message for clarity of the error. – Albin Sidås Jan 05 '22 at 11:37
  • 2
    _What_ type error? Give a [mre]. And note you're making this mistake: https://stackoverflow.com/q/15112125/3001761 – jonrsharpe Jan 05 '22 at 11:37
  • 2
    what is `I` at the last line? – qouify Jan 05 '22 at 11:39
  • Oh sorry, it was supposed to be a small i, but autocorrect made it capital. In the code it is a small i. – Soham Sharma Jan 05 '22 at 11:41
  • Pardon me, but I don't understand why `i = i+1` is required here. The loop works without it. – Anand Gautam Jan 05 '22 at 11:43
  • 2
    Please don't include pictures of text content - they can't be searched and indexed, don't scale like text would and are completely inaccessible to users with e.g. screen readers. But fundamentally the error tells you what the problem is - `i` is a _string_ (you literally just compared it to `"AUG"` so I guess you know that) so `+ 1` has no meaning when applied to it. – jonrsharpe Jan 05 '22 at 11:43
  • 1
    It seems that use `i` sometimes as a list index (`i = i + 1`) sometimes to refer to the list content (`i == "AUG"`). This can't work and you will naturally get `TypeError` exception, as the one you got. – qouify Jan 05 '22 at 11:46
  • Also, this line does not make sense much: `if i != "UAA" or "UAG" or "UGA":' I suppose you must write like this: `if i != "UAA" or 1!= "UAG" or i!= "UGA":` or you may choose to write it in a list format to get all conditions into one line: `if i == "AUG" or i not in ['UAA', 'UAG', 'UGA']:` Here is the result you get at the end: `UACGAUGUUUCGGGAAUGCCUAAAUGUUCCGGCUGC` – Anand Gautam Jan 05 '22 at 11:53
  • Please check ```str.index()``` : https://docs.python.org/3/library/stdtypes.html#str.index – Loïc Jan 05 '22 at 12:25

1 Answers1

1

The problem in this line of code:

if i != "UAA" or "UAG" or "UGA":
            hello += i

You should check each value of object i alone:

if i!= "UAA" or i!= "UAG" or i!= "UGA":
            hello += i

Or simply you can check for the condition:

 if i not in ["UAA", "UAG", "UGA"]:
           hello += i

Also you can't concatenate string value with int value in this line of code:

i = i + 1

You should cast 1 to string data type.

Oghli
  • 2,200
  • 1
  • 15
  • 37