Python Forum

Full Version: Error in my function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone. I try to make o function which takes 2 strings, a word and a letter, and counts how many times there is the letter in the word. Here is the code:

def count_nucleotides(dna, nucleotide):
""" (str, str) -> int

Return the number of occurrences of nucleotide in the DNA sequence dna.

>>> count_nucleotides('ATCGGC', 'G')
2
>>> count_nucleotides('ATCTA', 'G')
0
"""
count = 0


for char in dna:
if nucleodite in dna:
count = count +1
return count
print(count)

and here is the error message:

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
count_nucleotides('ATCGGC', 'G')
File "C:\Users\Giannis\Desktop\Python Assegments\Week 4\week4.py", line 51, in count_nucleotides
if nucleodite in dna:
NameError: name 'nucleodite' is not defined
Indentation is really important in Python, and if you don't enter your code between python tags in this board, then it is not displayed correctly, so it is hard to help you.
(Oct-01-2017, 07:36 PM)zafraj1 Wrote: [ -> ]NameError: name 'nucleodite' is not defined

Where in your code have you defined 'nucleodite'? (Which, by the way, should be nucleotide).
That aside:

I can't see where you call the function.

If you want a function to do something, you have to call it from somewhere and do something with the result (assign it to a variable or printing it, for example).

Also, if you want to check each character in turn to see if it matches you target, you need to test against the assigned character rather than the whole string.