user=input("name")
i=0
b=user.upper()
while i<len(b):
if (b[i]==b[i+1]):
print(f"{b[i]}:{b.count(b[i])}")
i+=1
The error message show the index out of range.
how to solve it.
user=input("name")
i=0
b=user.upper()
while i<len(b):
if (b[i]==b[i+1]):
print(f"{b[i]}:{b.count(b[i])}")
i+=1
The error message show the index out of range.
how to solve it.
First you must tell us what your code is supposed to do. You can not refer to i + 1
in b[i+1]
because when the while loop is in its last iteration, i + 1
would refer to an index which is beyond the max index of the string. Why? Because strings are zero-indexed just like arrays.
The program check to how many characters present in a name which is given by user.
From what you describe, it sounds like the code is supposed to print the number of characters in the name enter by user?