Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert list to int

I am trying to write a code to calculate the average of the height of the student
that is my code

 height = input("what is your higth\n").split()

for n in range(0, len(height)):

    height[n] = int(height[n])

print(height)


t = 0

for i in height:

    t += height

print(height)

and I have error

  t += higths

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use enumerate on the for loop to access to the list indexes and convert it to int.
Then calculate the sum divided by the len of the list.

height = input("what is your higth\n").split()

for i, element in enumerate(height) : 
    height[i] = int(element)

# Average
print(sum(height)/len(height))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading