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?

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
Average kid
  • 6,885
  • 6
  • 21
  • 17

3 Answers3

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.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
halex
  • 16,253
  • 5
  • 58
  • 67
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.
Community
  • 1
  • 1
K Z
  • 29,661
  • 8
  • 73
  • 78
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)
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525