+ 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
6 Réponses
+ 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))
+ 4
Question: Do you want to find a special character in the sentence or is it a word you want to find?
+ 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!!
+ 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))
+ 2
Thanks guys....i got it.. 😊😊