I am learning python. My current self taught assignment is to write a program that takes a string and checks weather or not the the letters in the string are in alphabetical order. If they are not, the program is to return the number of instances that the letters are out of order. I ask the help of stackoverflow, as the code to me looks good but I keep getting an error, that of which is included below.
the code
def inversions (string):
res = 0
for i in string:
if string[i+1] < string[i]:
res += 1
print( res)
my errors
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
inversions ('ghutinhjdbessadfg')
File "C:\Users\Christopher\Downloads\pratice.py", line 63, in inversions
if string[i+1] < string[i]:
TypeError: Can't convert 'int' object to str implicitly
I kinda get what the error is saying, but I am unsure of what to do about it.
Thank you very much