Output is right still it says that 'I didn't do it correctly


Also did the other way: To count for first letter of every word if letter is M

my_sentence = "My gosh, what a beautiful Monday morning this is."
def m_word_count(x):
  count = 0
  new_sentence = my_sentence.split()
  for i in new_sentence:
    if i[0].lower() == x.lower():
      count+=1
  return count
print(m_word_count('M'))

print(m_word_count('M')) prints 3, while it should it should print 1

your function only works for one sentence, not any string passed as argument to the function

Can you correct my code. I am not getting what do you mean. It is asking to show number of M’s as first letter is words in given sentence. I did the same. First split the sentence into words in a list than literate every first letter of word. 3 is what i got and it also asked for 3 as an output.

look at line 9 of your code:

print(m_word_count('M'))

clearly there is only one m in that string. Yet the function returns 3

if you add another function:

print(m_word_count('some more testing'))

we should get two (there are two m's in that string), yet we get 3 again.

you completely ignore your function parameter (x) and only make the function work for one global variable/string. Undermining the idea of functions and reusability.

I have used the string.lower() string method here to make every character of the first letter and the argument to lower case. So it doesn’t matter the argument is of which case similarly the string in the sentence variable in what case. Motive to count all M’s and m’s for first letter of every word

you have a function with a parameter:

def m_word_count(x):

the parameter (x) is not used anywhere.

instead you use my_sentence within the body of the function. Making your function only work for one global variable. Ignoring the parameter and argument.

not point my at all. I can’t take this, I am going to ask someone to take over. It seems I am not the right person to help you

EDIT: one final attempt, can you explain me how you think parameters and arguments work?

You need to count how many words there are in a given sentence that begin with m or M. So, if you pass in the sentence, "My favourite jam jars are blue and red.", should return 1. Since there is only one word in the sentence that starts with m or M.


Also, you need to make sure you know what the parameters mean. What value should x be?

sure,
here my_sentence is a global variable, so function can take this variable if same variable is not defined inside the function. I have not called my_sentence, it automatically took it from global.
and
x is a in scope variable. I have called x, which is a parameter and ‘M’ is an argument.

Ah. In this challenge, the x parameter is meant to be the input string. You don’t need the my_sentence variable at all. So, when calling the function, it should look like this:

m_word_count("My m word string")
#should return 2.
def m_word_count(my_sentence, x):
  new_sentence=my_sentence.split()
  count =0
  for i in new_sentence:
    if x.lower()==i[0].lower():
      count+=1
  return count
print(m_word_count("My gosh, what a beautiful Monday morning this is.", "M"))

You don’t need the my_sentence and x parameters. The only purpose of the function is to count the amount of words that begin with m.

def m_word_count():
  x = 'M'
  my_sentence = "My gosh, what a beautiful Monday morning this is."
  new_sentence=my_sentence.split()
  count =0
  for i in new_sentence:
    if x.lower()==i[0].lower():
      count+=1
  return count
print(m_word_count())

You shouldn’t be using fixed values for the sentence. The whole point of the function is to accept different sentences and check to see how many words begin with m. So, that should be a parameter, so you can call the function with different sentences. Don’t get fixed on the "My gosh, what a beautiful Monday morning this is." sentence. It’s only for example.

2 Likes

Your function should have one parameter. That parameter will be assigned to the argument passed to the function when the function is called. You should expect that argument to be a string. Your task is to write a function that accepts a string, and returns the number of words that begin with either ‘m’ or ‘M’ It’s possible the input string won’t contain any words that begin with ‘m’ or ‘M’. The SCT for the exercise uses additional test input that you don’t see on the screen.

my_sentence = "This could be any string value."

def m_word_count(any_string):
# parameter      ^^^^^^^^^^  # could be named anything you like
  #  code to count words beginning with 'm' or 'M' in any_string
  #  return count

m_word_count(my_sentence)
# argument   ^^^^^^^^^^^

#if we print the output using the current value of my string we should see 0 printed
2 Likes