kindrex735
kindrex735

Reputation: 55

Can someone tell me why my program works in Python 2.7 and not 3.3?

I have these two functions:

def MatchRNA(RNA, start1, end1, start2, end2):
    Subsequence1 = RNA[start1:end1+1]
    if start1 > end1:
        Subsequence1 = RNA[start1:end1-1:-1]
    Subsequence2 = RNA[start2:end2+1]
    if start2 > end2:
        Subsequence2 = RNA[start2:end2-1:-1]
    return Subsequence1, Subsequence2


def main():
    RNA_1_list = ['A','U','G','U','G','G','G','U','C','C','A','C','G','A','C','U','C','G','U','C','G','U','C','U','A','C','U','A','G','A']
    RNA_2_list = ['C','U','G','A','C','G','A','C','U','A','U','A','A','G','G','G','U','C','A','A','G','C']
    RNA_Values = {'A': 1, 'U': 2, 'C': 3, 'G': 4}
    RNA1 = []
    RNA2 = []
    for i in RNA_1_list:
        if i in RNA_Values:
            RNA1.append(RNA_Values[i])
    for i in RNA_2_list:
        if i in RNA_Values:
            RNA2.append(RNA_Values[i])
    RNA = list(input("Which strand of RNA (RNA1 or RNA2) are you sequencing? "))       
    Start1, End1, Start2, End2 = eval(input("What are the start and end values (Start1, End1, Start2, End2) for the subsequences of the strand? "))
    Sub1, Sub2 = MatchRNA(RNA, Start1, End1, Start2, End2)
    print(Sub1)
    print(Sub2)

So when I run the main function, and give as input (for example): RNA1, and then 3, 14, 17, 28, it should print two lists, [2,4,4,4,2,3,3,1,3,4,1,3] and [4,2,3,4,2,3,2,1,3,2,1,4]. I was inadvertently using Python 2.7 when I was testing this code, and it worked fine (without that eval in there), but when I run it in 3.3 (and put the eval back in) it prints two lists, ['1'] and []. Does anyone know why it doesn't work in 3.3 or how I can get it to work in 3.3? Thanks in advance.

Upvotes: 0

Views: 127

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251156

input() returns a string in Python3 while in Python2 it is equivalent to eval(raw_input).

For RNA1 as input:

Python3:

>>> RNA1 = []
>>> list(input("Which strand of RNA (RNA1 or RNA2) are you sequencing? "))
Which strand of RNA (RNA1 or RNA2) are you sequencing? RNA1
['R', 'N', 'A', '1']

Python2:

>>> list(eval(input("Which strand of RNA (RNA1 or RNA2) are you sequencing? ")))
Which strand of RNA (RNA1 or RNA2) are you sequencing? RNA1
[]

Upvotes: 3

Tim Peters
Tim Peters

Reputation: 70735

Close, but the main source of your problem is here:

 RNA = list(input("Which strand of RNA (RNA1 or RNA2) are you sequencing? "))

The list() call there is useless in any case.

In Python2, when you enter RNA1, input() evaluates that symbol and returns the list bound to RNA1.

In Python3, input() returns the string "RNA1", which the pointless ;-) list() turns into

['R', 'N', 'A', '1']

One way to change the code to run under both versions: first add this at the top:

try:
    raw_input
except:  # Python 3
    raw_input = input

Then change the input lines:

RNA = eval(raw_input("Which strand of RNA (RNA1 or RNA2) are you sequencing? "))
Start1, End1, Start2, End2 = eval(raw_input("What are the start and end values (Start1, End1, Start2, End2) for the subsequences of the strand? "))

Upvotes: 0

Related Questions