-2
student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
    student_heights[n] = int(student_heights[n])

total_height = 0
for height in total_height:
    total_height += height
print(total_height)

number_of_students = 0
for student in number_of_students:
    number_of_students += student
print(number_of_students)

avg_height = round(total_height / number_of_students)
print(avg_height)

i tried to make an avg height calculator but something isnt working

  • You really should provide more details. That said, `number_of_students` is an int (0), so the next line would expand to `for student in 0:` which is incorrect – Berend Nov 02 '22 at 08:03
  • Related: https://stackoverflow.com/questions/14941288/how-do-i-fix-typeerror-int-object-is-not-iterable – Thomas Weller Nov 02 '22 at 08:15

3 Answers3

1

You can do the split and parsing to int easier with map:

student_heights = list(map(int, input("Input a list of student heights ").split()))

Then, you do not have to iterate over the list, but use sum and len:

avg_height = round(sum(student_heights) / len(student_heights))

The TypeError you are getting is caused because you want to iterate in a for loop over total_height and number_of_students and they are ints, so it's not possible - you have to use iterable object for this.

yeti
  • 125
  • 1
  • 10
1

You are trying to iterate through integers, which isn't iterable.

Try this code:

student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
student_heights[n] = int(student_heights[n])

total_height = 0
for n in range(0, len(student_heights)):
total_height += student_heights[n]
print(total_height)

number_of_students = 0
for n in range(0, len(student_heights)):
number_of_students += 1
print(number_of_students)

avg_height = round(total_height / number_of_students)
print(avg_height)
0

You should iterator through list instead of variable int you just defined. Example.

student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
    student_heights[n] = int(student_heights[n])

total_height = 0
for height in student_heights:
    total_height += height
print(total_height)

number_of_students = 0
for student in student_heights:
    number_of_students = number_of_students  + 1
print(number_of_students)

avg_height = round(total_height / number_of_students)
print(avg_height)
GAVD
  • 1,977
  • 3
  • 22
  • 40