I am trying to calculate the no of times each character comes in the sentence.
message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
c = 0
for i in message:
a=0
for a in range(len(message) + 1):
if i == message[a]:
c+=1
print(str(i)+ ' comes ' + str(c) + ' times.')
c=0
len(message) + 1?range(len(message) + 1)not throw an index error? You took the length of the list of possible indices and then threw an extra one in that can't exist0tolen(message) - 1, which is why range() conveniently also only goes from0toend - 1. So remove the+ 1in your range() statement and this will resolve your IndexError. (There are other issues with your algorithm too, but this will take care of your immediate question.)cbefore use, not afterwards. Or simplyc = message.count(i).cbefore use