0

I'm getting "unsupported operand type(s) for *: 'float' and 'NoneType'" error for line 34: "s = (100 / words) * sentences".

Not sure what to make of this since count_sentences(text) does return a value.

Any help is greatly appreciated!

#import cs50

# Helper function to count letters only
def count_letters(text):
    letters = sum(c.isalpha() for c in text)
    return letters

"""Helper word and sentences counter function"""
def count_words(text):
    words = sum(s.isspace() for s in text)
    return words

def count_sentences(text):
    counter = 0
    for i in text:
        if i == ('!' or '?' or '.'):
            counter += 1
            print (counter)
            return counter



""" Readability - algorithm to assess writing level using Coleman-Liau index """

text = cs50.get_string("Text: ")  # Prompt user for text

""" Use helper methods to count letters, words, and sentences """
letters = count_letters(text)
words = 1 + count_words(text)
sentences = count_sentences(text)

""" Calculate letters and sentences per a hundred words """
l = (100 / words) * letters
s = (100 / words) * sentences

""" Coleman-Liau index index = 0.0588 * L - 0.296 * S - 15.8  """
grade = 0.0588 * l - 0.296 * s - 15.8

""" Cast to integer and round to nearest """
if grade > 16:
    print("Grade 16+")
    
elif grade < 1:
    print("Before Grade 1")
    
else:
    print(f"Grade: {int(round(grade))}")
2
  • Where are all the declarations? Is this just partial code?
    – Cliff B
    Oct 28, 2021 at 21:33
  • Its because of an indentation problem. Unindent the the line return counter in the count_sentences() function twice i.e, it should have only a single indentation (of 4 spaces/ a single tab).
    – an4s911
    Oct 31, 2021 at 10:40

2 Answers 2

1

Its because of an indentation problem. Unindent the the line return counter in the count_sentences() function twice i.e, it should have only a single indentation (of 4 spaces or a single tab). This should work:

import cs50

# Helper function to count letters only
def count_letters(text):
    letters = sum(c.isalpha() for c in text)
    return letters

"""Helper word and sentences counter function"""
def count_words(text):
    words = sum(s.isspace() for s in text)
    return words

def count_sentences(text):
    counter = 0
    for i in text:
        if i == ('!' or '?' or '.'):
            counter += 1
            print (counter)
    ################ Unindented the line below twice #######################
    return counter
    ########################




""" Readability - algorithm to assess writing level using Coleman-Liau index """

text = cs50.get_string("Text: ")  # Prompt user for text

""" Use helper methods to count letters, words, and sentences """
letters = count_letters(text)
words = 1 + count_words(text)
sentences = count_sentences(text)

""" Calculate letters and sentences per a hundred words """
l = (100 / words) * letters
s = (100 / words) * sentences

""" Coleman-Liau index index = 0.0588 * L - 0.296 * S - 15.8  """
grade = 0.0588 * l - 0.296 * s - 15.8

""" Cast to integer and round to nearest """
if grade > 16:
    print("Grade 16+")
    
elif grade < 1:
    print("Before Grade 1")
    
else:
    print(f"Grade: {int(round(grade))}")
1
  • 1
    Thank you! I just started on Python and I love it but the lack of curly braces with their scope definitions take a bit to get used to :-) Nov 2, 2021 at 20:15
0

It should not be difficult for you to check that the value of sentence is None, that is, your function count_sentences(text) is not returning anything, check the logic of your function to see what is wrong.

2
  • Thank you Cliff--except the print(text) command I think everything is present. I understand you mean the functions declarations? Oct 28, 2021 at 21:58
  • Thank you MARS--I used the below code for count_sentences instead, it works great (Python is awesome); but racking my brain over your answer I couldn't find the error in my logic. I'd love to learn why it didn't work? This is what I used instead: def count_sentences(text): sentences = text.count('.') + text.count('?') + text.count('!') return sentences Oct 28, 2021 at 22:32

You must log in to answer this question.

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