0

I'm trying to calculate the average length of sentences in a .txt file by dividing the number of words by number of full stops:-

def divide(x, y):
    return x / y

file = open("File One.txt", "r")
data = file.read()

words = data.split()
print('Number of words in text file:', len(words))

count = data.count('.')
print("Number of full stops:", count)

num1 = words
num2 = count

print("Average sentence length:", divide(num1, num2))

But I end up with:-

Number of words in text file: 25
Number of full stops: 3
Traceback (most recent call last):
  File "C:\Users\Jonathan\Desktop\Data\stripped down.py", line 16, in <module>
    print("Average sentence length:", divide(num1, num2))
  File "C:\Users\Jonathan\Desktop\Data\stripped down.py", line 2, in divide
    return x / y
TypeError: unsupported operand type(s) for /: 'list' and 'int'

Any help or advice would be appreciated.

  • Your `num1` is defined to be the word list itself rather than its length. This question is basically a typo. Also , what is the point of `divide`? Defining a function rather than just using the operator is more typing which just makes the code less readable. – John Coleman Feb 27 '21 at 11:34

2 Answers2

0

you are directly passing list num1 as it is, but you need assign its length to num1

def divide(x, y):
    return x / y


file = open("File One.txt", "r")
data = file.read()

words = data.split()
print('Number of words in text file:', len(words))

count = data.count('.')
print("Number of full stops:", count)

num1 = len(words)
num2 = count

print("Average sentence length:", divide(num1, num2))

sample Output:

Number of words in text file: 9
Number of full stops: 12
Average sentence length: 0.75
Lohith
  • 866
  • 1
  • 9
  • 25
0

print("Average sentence length:", divide(num1, num2)) calls def divide(x, y). num1 is a list, as can be seen here:

data = file.read()
words = data.split()

For example if your string was data = "abc def ghi jkl", data.split() will return a list (['abc', 'def', 'ghi', 'jkl']).

In your specific example, you could use len() to access the length of said list - you will therefore have an int, which in the end is dividable, so:

#[...]
num1 = len(words)
num2 = count

print("Average sentence length:", divide(num1, num2))
J. M. Arnold
  • 6,261
  • 3
  • 20
  • 38