0
PYTHON. count the word in text by using def.
THIS IS MY CODE. PLEASE HELP ME FIX IT. ( there are 2 input: text and word) def word_count(text, word): if word in text: return text.count(word)) text = input() word = input() x=word_count(text, word) print(x)
5 Answers
+ 8
here is an similar solution:
def word_count(text, word):
return text.count(word)
text = input().split()
word = input()
print(word_count(text, word))
+ 3
you doesn't need the 'if' statement in your code (if not word in text, you'll return None instead of 0)...
however, your logic is not good: counting 'java' in 'javascript' would report 1, while 0 is expected ^^
you must split the text into words, and count occurence of target word inside the array ;)
+ 3
You have excess closing parentheses in `word_count` function return statement.
return text.count(word) # <- remove excess ')'
+ 3
If you don't use def, just
print(input().split().count(input()))
0
Try this :
def word_count(text, word):
if word in text:
res = text[0:].split(' ')
return res.count(word)
text = input()
word = input()
print(word_count(text, word))