0

This is my code (Python 3.2)

Total = eval(input("How many numbers do you want to enter? "))
#say the user enters 3
for i in range(Total):
    Numbers = input("Please enter a number ")
#User enters in 1
#User enters in 2
#User enters in 3
print ("The sum of the numbers you entered is", Numbers)
#It displays 3 instead of 6

How do i get it to add up properly?

3 Answers 3

3

Just a quick and dirty rewrite of your lines:

Total = int(input("How many numbers do you want to enter? "))
#say the user enters 3
Numbers=[]
for i in range(Total):
    Numbers.append(int(input("Please enter a number "))
#User enters in 1
#User enters in 2
#User enters in 3
print ("The sum of the numbers you entered is", sum(Numbers))
#It displays 3 instead of 6

I assume that you use Python 3 because of the way you print, but if you use Python 2 use raw_input instead of input.

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

1 Comment

Just letting you know, I edited your answer before I realised it was merely reflecting the question. Not cool. Sorry.
2

This code will fix your problem:

total = int(input("How many numbers do you want to enter? "))
#say the user enters 3
sum_input = 0
for i in range(total):
    sum_input += int(input("Please enter a number "))
#User enters in 1
#User enters in 2
#User enters in 3
print ("The sum of the numbers you entered are", sum_input)

A number of comments:

  1. You should stick to pep8 for styling and variable names. Specifically, use under_store for variable names and function names, and CapWords for class names.
  2. The use of eval is questionable here. This article explains very well on why you shouldn't use eval in most cases.

1 Comment

Reusing total is even worse.
1

You need to declare your variable outside the for loop, and keep on adding input numbers to it in the loop..

numbers = 0
for i in range(Total):
    numbers += int(input("Please enter a number "))

print ("The sum of the numbers you entered are", numbers)

Comments

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.