1

My homework assignment is to calculate the sum of a series of numbers inputted by the user using a 'For Loop', but I can't seem to successfully add the input numbers.

I have tried printing the variable that holds the amount of numbers the loop repeats, and using things like "1 + 2 + 3 + 4 + 5", but it either prints every time the code loops, or it prints "15", for example. This is the code:

listo = (1,2,3,4,5)

for num in range(len(listo)) :
 float(input("Enter a number: "))
 krab = #This is where I'm struggling, as I don't know how to add the inputted numbers.
print "Your total sum is" , krab

The output should be the sum of each time it loops, so if the inputted numbers are, for example, 5 through 10, the program should print "35".

2
  • What else did you expect to be the result of 1 + 2 + 3 + 4 + 5? Commented Apr 7, 2019 at 2:18
  • @KlausD. I want to sum the numbers of the inputs, not the actual numbers. Sorry if that was confusing. Commented Apr 7, 2019 at 2:24

1 Answer 1

1
listo = [1,2,3,4,5] #or you can just do x=5

krab = 0.0

for n in range(len(listo)) :   #range(0,x):

      num = float(input("Enter a number: "))

      krab = num + krab '''this will add the provided number with the present value of krab'''

 print("Your sum is " , krab)

Sign up to request clarification or add additional context in comments.

4 Comments

This actually worked, but the code added each number with 'krab' (sorry for the weird name, haha) instead of the 5 numbers being added. Thank you!
Which five numbers do you want to be added? The ones in listo or the ones in the input?
The numbers in the input
It will add the inputed numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.