I am trying to write a program to count the occurrences of a specific letter in a string without the count function. I made the string into a list and set a loop to count but the count is never changing and i cant figure out why. This is what I have right now:
letter = 'a'
myString = 'aardvark'
myList = []
for i in myString:
myList.append(i)
count = 1
for i in myList:
if i == letter:
count == count + 1
else:
continue
print (count)
Any help is greatly appreciated.
count == count + 1is not an assignment. Simply remove one=.'aardvark'.count('a')does the job better and faster? (At least it isn't as errorprone)forloop features anif...else...where theelsefeatures acontinue. Thecontinueis not necessary because there is nothing to avoid computing with theforloop. A solitaryifwould suffice.for char in myString: ...does the job and is very Pythonic at that.