Problem with IF / ELSE and variables

1

I started to study Python very soon, and created the following code:

gene = open("AY365046.1.txt","r")

g=0;
a=0;
c=0;
t=0;

gene.readline()

for line in gene:
    line = line.lower()
    for char in line:
        if char == "g":
            g+=1
        if char == "a":
            a+=1
        if char == "c":
            c+=1
        if char == "t":
            t+=1

print "Guanina: " + str(g)
print "Adenina: " + str(a)
print "Citosina: " + str(c)
print "Timina: " + str(t)

gc = (g+c+0.) / (a+t+c+g+0.)

print "Conteúdo GC: " +str(gc)

I created it based on a basic tutorial, and adapted as I wished ...

Now I want to make it interactive ... My goal is to use the input () function to get the sequence number in which the data will be displayed ...

In the code above, it gets only the data of a sequence (AY365046.1.txt) ... So I need the code to have access to more .txt files (such as sequence2.txt and string3.txt ), and get it data for g , a , c strong> t of the file entered in input () ...

Example:

1) System asks for sequence number

2) User reports sequence2

3) System get information from sequence2.txt

g , a , c and t / strong> and displayed through print ()

5) If a non-existent sequence, such as sequence5 is reported, an error message is reported ...

As far as I understand, to accomplish all this I just have to declare the variables, assign the .txt files to each of them, and create an if / else ...

The problem is that I tried in every way to figure out how to do this, and I could not ...

Obviously you do not need to create the code for me, but ... Can you tell me at least where to start? Is my reasoning correct? Something missing?

Thank you!

    
asked by anonymous 14.08.2015 / 10:04

1 answer

1

Your code is pretty much ready, what's left is to put it inside a loop and get the file name of the user.

It would look something like:

run = True
while run:
    # Pega sequência do usuário
    sequence = input("Insira a sequencia:")

    # Tenta abrir o arquivo da sequencia
    try:
        gene = open("sequence ","r")
    catch IOError:
        print("Sequencia nao encontrada")
        # Interrompe essa iteracao e vai para a proxima
        continue

    # Todo o resto do teu código aqui
    # Quando terminar, seta o run pra False para interromper o loop

Remember to always close the file at the end of the loop

gene.close()
    
14.08.2015 / 18:45