Write a Python Program to Add N Numbers Accepted from the User

Adding n numbers is a common task in Python, so in this tutorial, you will learn how to write a Python program to add n numbers accepted by the user.

You will learn about several methods and where you can implement this concept in real life. For example, you can implement totalling the price of items in the cart, which contains n items.

Write a Python Program to Add N Numbers Accepted from the User

In Python, you can achieve this by writing a program to add n numbers accepted by the user using the with and without functions.

I will be explaining both the approach here. Now, here the logic is very simple: first, a program will determine how many numbers you want, like 3, 5, 8, etc. After that program asks for each number one by one that you want to add.

For example, if you specify that you want to add four numbers, the program will ask you four times for a different four numbers.

Write a Program to Find the Sum of n Numbers in Python

To add or find the sum of n numbers, suppose you want to add 2 numbers like 5 or 6; you can use the code below.

# Accepting the total number user want to add
num = int(input("Enter the number of numbers you want to add: "))

# empty list to store all the number entered by the user
n_numbers = []

# for loop to accept the number from the user and add it to the empty list
for i in range(num):

  # accepting input from the user in each iteration
  number = int(input(f"Enter the {i+1} number: ")) 

  # adding each input number to the n_numbers list using append() method
  n_numbers.append(number)

# intializing a with 0 to store the sum of all the given numbers
total = 0

# Running loop to take each number from the n_numbers
for current_num in n_numbers:

  # In each iteration adding each numbers to the total
  total = current_num + total

print(f"Total of given {num} number is : ", total)
Write a Python Program to Add N Numbers Accepted from the User

From the output, the user enters 2 numbers: 5 and 6. Then, both numbers are added, and the sum of the given number is 11.

READ:  How to convert string to boolean in Python? [6 Methods]

Sum of N Numbers in Python using Function

You can also add n numbers using the function; Python has a built-in function named sum(), which adds the given number.

For example, you can modify the code of the above section as shown below.

# Accepting the total number user want to add
num = int(input("Enter the number of numbers you want to add: "))

# empty list to store all the number entered by the user
n_numbers = []

# for loop to accept the number from the user and add it to the empty list
for i in range(num):

  # accepting input from the user in each iteration
  number = int(input(f"Enter the {i+1} number: "))

  # adding each input number to the n_numbers list using append() method
  n_numbers.append(number)

# using the sum() function to add the given n numbers
total = sum(n_numbers)


print(f"Total of given {num} number is : ", total)
Sum of N Numbers in Python using Function

Look, this time, the sum() function is used to add the n numbers; in the above code, the n_numbers list is passed to the function like this sum(n_numbers), and as a result, it adds the numbers and returns the total as 21.

Also, this time, 4 numbers [5, 8, 7, 1 ] are added.

The Sum of N Numbers in Python using For Loop

You can use the single for loop to find the sum of n numbers. For example, run the code below.

# initialize the sum to 0
total_sum = 0

# ask the user how many numbers they want to add
n = int(input("Enter the number of values you want to add: "))

# loop to take the numbers from the user
for i in range(n):
    num = float(input(f"Enter number {i+1}: "))
    total_sum += num

# print the final sum
print(f"The sum of the {n} numbers is: {total_sum}")
The Sum of N Numbers in Python using For Loop

The user enters three numbers from the output: 34, 20, and 50. Then, using the for loop, three numbers are added; the sum is 104.0.

READ:  Python Remove a Newline from the String

Conclusion

In this Python tutorial, you covered how to write a Python program to add n numbers accepted by the user.

You used the double loop and single loop to add n numbers accepted by the user and also used functions like sum() to sum the n numbers.

You may like to read: