0

I'm kind of stuck on this challenge

the objective is: "Create a function that counts how many D's are in a sentence."

Some examples:

count_d("My friend Dylan got distracted in school.") ➞ 4

count_d("Debris was scattered all over the yard.") ➞ 3

count_d("The rodents hibernated in their den.") ➞ 3

Here's my current code:

def count_d(sentence):
    print(sentence)
    sentence = sentence.lower
    substring = "d"
    return sentence.count(substring)

When I run it, the console sends an error message:

ERROR: Traceback:
   in <module>
   in count_d
AttributeError: 'builtin_function_or_method' object has no attribute 'count'
Mika Tan
  • 118
  • 9
  • 1
    `sentence = sentence.lower()` call the function. – user2390182 Oct 30 '20 at 11:53
  • 1
    Does this answer your question? [Count the number occurrences of a character in a string](https://stackoverflow.com/questions/1155617/count-the-number-occurrences-of-a-character-in-a-string) – Antoine Dubuis Oct 30 '20 at 11:55

2 Answers2

1

lower() instead of lower only. You want the method to return the value, not to get the method itself

Alvi15
  • 335
  • 2
  • 10
0

As already note you need to call method not get method itself. I want to add that you might chain str methods i.e.:

def count_d(sentence):
    print(sentence)
    substring = "d"
    return sentence.lower().count(substring)

Depending on situation this might be more readable than doing one action per one line.

Daweo
  • 31,313
  • 3
  • 12
  • 25