[Solved] How to fix this: ValueError: invalid literal for int() with base 10: [closed]


the default behaviour of split() is to split by whitespace. You code would not throw errors if the input only contained numbers and whitespaces, e.g. 156 178 165 171 187.

If you want a different split seperator, e.g. the comma ,, you could use split(",") instead.

Valid input would be e.g. 156,178,165,171,187.

Note that [156,178,165,171,187] is not valid input. It would split the string only at the comma, resulting in the first element to be equal to [156, clearly not a valid number.

If you want to skip the input step and use a list instead, replace:

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

with:

student_heights = [156,178,165,171,187]

If you realy need to pass [156,178,165,171,187] as an input, you could also use literal_eval to convert it to a list. Don’t forget to import the required module:

from ast import literal_eval
student_heights = literal_eval(input("Input a list of student heights "))

for n in range(0, len(student_heights)): 
  student_heights[n] = int(student_heights[n])

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

1

solved How to fix this: ValueError: invalid literal for int() with base 10: [closed]