Home > Software design >  How to sum the entered values with user input
How to sum the entered values with user input

Time:11-21

First, the program asks the user the amount of numbers to sum.(n) Then the user enters values in amount(n), lastly the entered values gets summed and should be printed out. I written the base of the code, but can't find how I can sum all the entered values into one.

Input: 5, 2, 4, 6, 8, 10
Output: 30

Here is my code:

n = int(input("Amount of numbers to sum: "))
for i in range (0,n):
  s = float(input("Enter number: "))

Would appreciate some recommendations regarding this.
Edit: Solved, thankyou all for the fast responses!

CodePudding user response:

Create a new variable outside the loop and set its value to 0. Inside teh for loop, add the input number to the variable.

final_sum = 0

for i in range (0,n):
  s = float(input("Enter number: "))
  final_sum = final_sum   s    #or use final_sum  = s

print(final_sum)
  • Related