0
numbers = input("give me al list of numbers: ")
list_number = []

for i in numbers:
  if i.strip(" "):
    list_number.append(i)

print(list_number)

Up to here everything works, but I try to calculate the average and I get this error:

# Traceback (most recent call last):

# TypeError: 'list' object is not callable

I have tried a for loop:

for i in list_number():
  average = int(list_number[i])

I tried a simple math calculation storing it in a var but nothing.

average = sum(list_number) / len(list_number)

# error: TypeError: unsupported operand type(s) for +: 'int' and 'str'

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Are you using Python 2 or 3? – Nick ODell Aug 17 '23 at 16:03
  • I removed the misleading [tag:python-2.7] tag; the syntax here is clearly Python 3. – tripleee Aug 17 '23 at 16:10
  • You can easily convert the string to `int` but you need to split it: `list_of_numbers = [int(i) for i in numbers.strip().split()]` – tripleee Aug 17 '23 at 16:15
  • If you want to loop through `list_numbers` by item, remove the `()` so just `for value in list_numbers:`. If you want to iterate through using an index, use `for index in range(len(list_numbers))`. – Brian McFarland Aug 17 '23 at 16:15
  • (Though probably don't use the `index` variant unless your code specifically needs to know the index for some reason. You see a lot of code which needlessly does this because that's the only way to do it in some lower-level languages like C or Java.) – tripleee Aug 17 '23 at 16:25

0 Answers0