简体   繁体   中英

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!

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)

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM