Practice Strings - m_word_count question

Hi all.

I have been doing the practice questions for strings in Python. Here is one that I’m stuck with conceptually. Found here.

Here is the specific question:

Write a function called m_word_count that takes a string as an input and returns a count of the words in the string that start with the letter M.

For example, if my_sentence = "My gosh, what a beautiful Monday morning this is." , then m_word_count(my_sentence) should return 3 .

My code is below:

my_sentence = "My gosh, what a beautiful Monday morning this is."

def m_word_count(strng):
  count = 0
  ms = my_sentence.upper().split(" ")
  for word in ms:
    if word[0] == "M":
      count += 1
  return count


My thought process is as follows:
Split string into seperate items in a list with uniform case to prevent differing ASCII values, then I will iterate through the list and create a condition if list item begins with “M” using index of [0]. If it is true, it will add 1 to the count variable until all items in list have been iterated.

My code above returns “3”, which is what should be returned. I tried adding extra words to the “my_sentence” string with upper and lower case "M"s to test if my function would work outside of the given string.

It gives the right count, but I am still getting an error:

Your m_word_count function did not correctly count the number of m s

This leads me to believe that the way I have constructed my function is incorrect.
Can someone please explain to me why my function might not be accepted by the tests they have in place for this question?

And also, if my thought process function is conceptually flawed?

Your function may depend a little on the input you give it. More complex strings could throw a spanner in the works. Try and think of any ways to break this function. Do you have a link to the lesson/question itself?

Hello @bulbsofpassion and welcome to the Codecademy Forums!

I noticed that you didn’t use the strng parameter in your function body, but rather used my_sentence. Codecademy tests your code using several different strings to see if your code returns the correct result.

If you use ms = my_sentece.upper().split(" ") instead of ms = strng.upper().split(" "), your function will always return 3 since it always iterates through my_sentence. Make sure that your function will return the correct value for any string, not just the one in the example.

I this problem, I used this:

my_sentence = “My gosh, what a beautiful Monday morning this is.”

def m_word_count(string):
counter = 0
for letter in string:
if letter in “Mm”:
counter += 1
return counter


and it came up correct.

However, the instructions are for only if the word starts with M or m.

If I put extra M’s in the middle of words, it still counts them. I think the original poster was trying to avoid that error. Is that the way to do it? To spilt the string and index the letter like if word[0] == M

Also, I’m not sure how to format my post in here to show my actual indenting for the code like others are doing. Sorry about that.

That seems like a reasonable idea, give it a shot :slightly_smiling_face:.

For formatting guidance see- How do I format code in my posts?