Why does this code give me all the number of items in the string...rather than the number of the particular word thathas to inpu | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why does this code give me all the number of items in the string...rather than the number of the particular word thathas to inpu

https://code.sololearn.com/c9eI464BQA0B/?ref=app

12th Jun 2019, 4:30 PM
Y AD Ù
Y AD Ù - avatar
6 Answers
+ 2
Your condition "char == y" is always true (because you set char = y in line 11). x = input() y = input() def read(text,char): cntr=0 for i in text: if i == char: cntr += 1 return "There are ", cntr, char,"in this text" print(read(x,y))
12th Jun 2019, 5:06 PM
Diego
Diego - avatar
+ 4
Question: Do you want to find a special character in the sentence or is it a word you want to find?
12th Jun 2019, 4:45 PM
Lothar
Lothar - avatar
+ 4
Choe, really nice code! i was not aware that i can split() string direct in for loop. i allways did it direct at input(). Also a good idea to form a string and directly return it from func! Good stuff!!
12th Jun 2019, 8:34 PM
Lothar
Lothar - avatar
+ 2
if you want to find words in a sentence x=str(input()) y=str(input()) def read(text,word): cntr=0 for i in text.split(" "): if i == word: cntr+=1 return "there are ", cntr, word,"in this text" print(read(x,y))
12th Jun 2019, 6:56 PM
Choe
Choe - avatar
+ 2
Lothar aww thanks 😄 this is Y Åđ Û code though, just made some changes
12th Jun 2019, 8:42 PM
Choe
Choe - avatar
+ 2
Thanks guys....i got it.. 😊😊
13th Jun 2019, 1:09 AM
Y AD Ù
Y AD Ù - avatar