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

Unable to sum numbers in a list – getting int is not iterable

We have to write a python program to read through a pre-set list and find the sum. No matter what i do it never works. The code i currently have is:

number_list = [425, 576, 945, 1086, 15]   
print("There is a list of numbers as follows:")  
for items in number_list:  
     print(items)  
total =sum(items)  
print("the sum of the numbers in the list is: ", total)

>Solution :

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

You need to pass to the sum() function the name of the list that you want to find the sum of its elements, not the items variable, which only contains the value of the last item:

number_list = [425, 576, 945, 1086, 15]
print("There is a list of numbers as follows:")
for items in number_list:
    print(items)
total = sum(number_list)
print("the sum of the numbers in the list is: ", total)
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