Skip to content

Write a Python program to sum all the items in a list input by the user | Code

  • by

First Read the input number asking for the length of the list using the input() function and store these values into a list. After that, you can use the sum() function or Iterate each element in the list using for loop and sump up all.

Python program to sum all the items in a list input by the user

Simple example code.

Using sum() method 

lst = []
num = int(input('How many numbers: '))

for n in range(num):
    numbers = int(input('Enter number '))
    lst.append(numbers)
    
print("Sum of elements in given list is :", sum(lst))

Output:

Write a Python program to sum all the items in a list input by the user

Using for loop with range function

lst = []
total = 0
num = int(input('How many numbers: '))

for n in range(num):
    numbers = int(input('Enter number '))
    lst.append(numbers)

for ele in range(0, len(lst)):
    total = total + lst[ele]

print("The Sum of user input value is :", total)

Output:

How many numbers: 3
Enter number 1
Enter number 2
Enter number 3
The Sum of the user input value is: 6

Do comment if you have any doubts or suggestions on this Python program.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.