Galadriel
Galadriel

Reputation: 95

For loop prints only last value in Python

I am a newbie at programming, and I have been learning Python for a very short time. Below, I tried to write a code, which counts nucleotides in a sample DNA sequence (A question from ROSALIND).

nucleotides=['A','C','G','T']

string='AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'

    for n in nucleotides:

        a = string.count (n)

        print ("The count for",n,"is:",a)

The output is:

The count for T is: 21

The problem is that my code prints only the result of last element in "nucleotides" array, which is 'T'. I know that I am asking a silly question but I tried to find an answer by searching on both here and web, and I wasn't successful. This is why, I would be very appreciated if you could correct the code and made an explanation to me why my loop did not print counts for each nucleotide.

Many thanks!

Upvotes: 1

Views: 10064

Answers (4)

Daedalus
Daedalus

Reputation: 416

Your code is actually working, except the fact you add an extra tab in the for loop (wrong indentation). You could try this slightly improve variation instead:

# define nucleotides 
nucleotides=['A','C','G','T']
# define dna chain
string='AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'

# iterate through the dna chain and count 
# the number of appearances for each nucelotide.
for nucl in nucleotides:
    x = string.count(nucl)
    print ("The count for " + nucl + " is: " + str(x))

Upvotes: 1

Sheldore
Sheldore

Reputation: 39052

Your issue was the indentation as pointed out by other answers.

Alternatively, you can use Counter from collections to get a dictionary containing the frequency of occurrence of each letter. Then just loop over your nucleotides to print the frequency.

from collections import Counter

nucleotides=['A','C','G','T']
string='AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
counts = Counter(string)

for n in nucleotides:
    a = counts[n]
    print ("The count for",n,"is:",a)

Output

The count for A is: 20
The count for C is: 12
The count for G is: 17
The count for T is: 21

Upvotes: 2

PyHomer
PyHomer

Reputation: 79

I tried the code on sublime and got the following result.

('The count for', 'A', 'is:', 20)
('The count for', 'C', 'is:', 12)
('The count for', 'G', 'is:', 17)
('The count for', 'T', 'is:', 21)

I think the problem with your code is that you indented "for loop" unnecessarily. make sure to use the right indentation.

Upvotes: 0

Tom Ron
Tom Ron

Reputation: 6181

I would check the indentation in your code as it is incorrect in your question. This snippet should work.

nucleotides=['A','C','G','T']

string='AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'

for n in nucleotides:
    a = string.count (n)
    print ("The count for",n,"is:",a)

Upvotes: 3

Related Questions