The error I am getting

Traceback (most recent call last):
  File "/home/ubuntu/pset/pset6/readability/readability.py", line 46, in 
  File "/home/ubuntu/pset/pset6/readability/readability.py", line 20, in 
    if user_input[i].isalnum():
TypeError: string indices must be integers

Here is my code:

def main():

    letters = 0
    words = 1
    sentences = 0
    avgletter = 0
    avgsentence = 0
    index = 0

    user_input = input("Text: ")

    #'avgletter' refers to average of number of the letters.
    #'avgsentence' refers to average of number of the sentences.

    length = len(user_input)

    for i in user_input:
        if user_input[i].isalnum():
            letters = letters + 1

        if user_input[i] == ' ':
            words = words + 1

        elif user_input[i] == '.' or  user_input[i] == '?' or  user_input[i] == '!':
            sentences = sentences + 1

    avgletter = (letters / words) * 100

    avgsentence = sentences / words * 100

    index = 0.0588 * avgletter - 0.296 * avgsentence - 15.8

    if index > 16:
        print(" Grade 16+")

    elif index < 1:
        print("Before Grade 1")

        print("Grade %d", int(round(index)))

    print(" ")


Please help me rectify where I am going wrong.

1 Answer 1


length is a single value: an integer equal to len(user_input). You cannot iterate through a single value, so Python will throw an error for for i in length. You can either use a standard loop where you keep iterating through the loop until i == length (while i < length... as far as I am aware, Python does not have the standard for loop structure that one might be used to from C-based languages) or you can iterate through all of the letters in user_input (for i in user_input). Applying one of these solutions might require you to adjust the logic or code of your to some degree to match how it is now working.

Remember that, in Python, this is the syntax for what other languages call a "foreach" loop. So, Python will iterate though the loop for each i in length. But, there is only one thing contained in length: that single integer value. So, Python shows an error.

Hopefully this helps. If it does, you can accept my answer by clicking the checkmark beside it. But if not, let me know and I will try to help further.

  • So what would you suggest me to improve my code? Jul 16, 2021 at 17:30
  • I gave two suggestions in my third sentence. You could use a while loop and likely change very little about your loop's logic, or use the same loop you have, but structured as for i in user_input. Keeping with a for loop might require some more work to your loop's code itself, but is also a more natural Python approach, in my experience. Either way will work, if you make sure that your loop's logic matches that of its declaration. Jul 17, 2021 at 19:02
  • I improved my code but now it is giving another error. I have updated my question. Please look into it Jul 18, 2021 at 16:31
  • That's because there is no such thing as user_input[i] in your loop. i is an object and not an integer. You will need to change the code in your loop to match how your loop is declared. Read up on (it's a quick read) "for loops in Python" (or "foreach loops," as this is what other languages call them) to understand how your logic needs to be for your loop. As I stated in my post and comment, after you change your loop declaration, you will need to make sure that your logic matches--there isn't really a cut/paste solution. Jul 18, 2021 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .