0

I am working on problem set 6 - Readability (python). When I am compiling my code the following error keeps coming.

Traceback (most recent call last):
  File "/home/ubuntu/pset/pset6/readability/readability.py", line 1, in <module>
    from string import strlren
ImportError: cannot import name 'strlren' from 'string' (/usr/local/lib/python3.9/string.py)

And this is the code I wrote:

from string import strlren

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.

    i = 0
    n = strlen(user_input)

    while i < n:
        if isalnum(user_input[i]):
            letters = letters + 1

        elif 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")

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

    print(" ")

main()

Please help me rectify my error.

Fatmah Faridi.

0

You don't need to use from string import strlren (it won't work and not just because it would be strlen). string is a data type and not a library.

Python has len built-in, which is what you are looking for. You'll use it as you would strlen in C.

Hope that helps. If it does, feel free to mark the check next to my answer to accept it. But if not, let me know and I will try to help further.

You must log in to answer this question.

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