I have not long been learning python and after going through the basics have been going through some tasks I have found online.
For this one I'm to use a list of characters in this case the alphabet to count the amount of those characters in a sentence.
I have got to the point where it is counting something but cuts out on character c giving me a value of 30 when there is only 1 c in there and the return:
File "/home/pi/main.py", line 17, in <module>
if statementLower[l] == alphabet[m]:
IndexError: string index out of range
Code attached
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
statement = "This is my task I want to complete. But I am stuck!!!!!!"
statementLower = statement.lower()
m = 0
l=0
count = 0
print(statementLower)
for i in alphabet:
frequency = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
#print("i for alphabet")
#print("-" + i + "-")
for k in statementLower:
#print("k statement Lower")
#print(k)
if statementLower[l] == alphabet[m]:
#print(count)
count = count+1
frequency[m] = count
l+l+1
continue
else:
#print("else continue loop")
l=l+1
continue
if frequency[m] != 0:
#print("frequency loop")
print(str(alphabet[m]+ ":" + str(frequency[m])))
m=m+1
count = 0
l=0
continue
Can anyone give me any assistance in what I'm doing wrong I can see that I am constantly getting stuck in that count if statement but its not incrementing to the next index of the string even when I am increasing that value.
my only current returns are a:44 b:20 c:30 then the string index out of range