Problem with IF/ELSE and variables

Asked

Viewed 494 times

1

I started studying Python a short time ago, 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 using a basic tutorial, and adapted it as I wanted...

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

In the above code, it only obtains data from a sequence (AY365046.1.txt)... Therefore, I need the code to have access to more files. txt (for example sequence 2.txt and sequence 3.txt), and that it obtains the data of g, to, c and t of the file on input()...

Example:

1) System asks sequence number

2) User informs sequence 2

3) System get information from sequence 2.txt

4) g, to, c and t are obtained from the sequence 2.txt and exhibited through the print()

5) And if a non-existent sequence is reported, for example, sequence 5, an error message is reported...

As far as I understood, to accomplish all this just I 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 accomplish this, and I couldn’t...

Obviously you don’t need to create the code for me, but... Can you at least tell me where to start? Is my reasoning about what I should do correct? Something is missing?

Thank you!

  • 1

    You should start by posting what you have tried and the problems you have had. Otherwise it only remains to do for you since you have defined how to do. Tips: Try to solve one problem at a time. Even the most experienced programmers do this. First make the file name be requested for the user (step 1). Step 2 is not something you put in the code, forget it. The 3 you already do, all you need is the name to be the obtained variable. The 4 already does. The 5 is another problem and you have not defined what is a non-existent sequence. Put what you did and I’ll give you an answer.

  • I suggest you use Python 3 which is the future of the language. The changes you will have to make to make this script work are minimal. In your case just change the prints of (example) print "banana" for print("banana")

1 answer

1

Your code is almost ready, what remains is to put it inside a loop and receive the name of the user file.

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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.