So for example if i want to know the number of times hello occurs in this word: hellohellothere, my code will give me 2 which is correct. But if i were to have hellotherehello, my code will not give me 2, which means i think there is something wrong with my second for loop. My code counts the number of letters in the string, and then i divide it by the length of the string to give how many times the string actually occurs, i don't think that really is the problem though.
here is the code.
word = input("Enter a word: ")
find = input("Enter string to find")
count = int(0)
for x in range(0, len(word)-len(find)):
if word[x] == find[0]:
for i in range(0, len(find)):
if word[x+i] == find[i]:
count += 1
else: break
count = count/len(find)
print("Number of times it occurs is: ", count)
Aucun commentaire:
Enregistrer un commentaire