Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in my function
#1
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
Reply
#2
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.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
(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).
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
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.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020