1

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!

2
  • Python is sensitive to indentation. Make sure that the print statement is indented relative to the for statement. Commented Dec 23, 2018 at 17:46
  • 1
    does the indentation here match the indentation in your code? i suspect the code you have has the print statement outside the for loop, but it does not look that way here. Commented Dec 23, 2018 at 17:46

4 Answers 4

3

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)
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.