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)

16th Mar 2021, 6:54 PM
Háș­u Tráș§n Văn
Háș­u Tráș§n Văn - avatar
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))
16th Mar 2021, 8:38 PM
Lothar
Lothar - avatar
+ 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 ;)
16th Mar 2021, 7:03 PM
visph
visph - avatar
+ 3
You have excess closing parentheses in `word_count` function return statement. return text.count(word) # <- remove excess ')'
16th Mar 2021, 7:08 PM
Ipang
+ 3
If you don't use def, just print(input().split().count(input()))
16th Mar 2021, 11:44 PM
David Ashton
David Ashton - avatar
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))
16th Mar 2021, 7:51 PM
Houssem Ben Youssef
Houssem Ben Youssef - avatar