How do I add five numbers from user input in Python?
Question:
As a practice exercise, I am trying to get five numbers from a user and return the sum of all five number using a while loop. I managed to gather the five numbers, but the sum is not provided by my code (I get a number, but it is always double the last number). I believe the issue is with my use of +=.
x = 0
while x < 5:
x += 1
s = (int(raw_input("Enter a number: ")))
s += s
print s
Answers:
This should be better.
x = 0
s = 0
while x < 5:
x += 1
s += (int(raw_input("Enter a number: ")))
print s
You were putting one of the results on to the sum of all results and lost the previous ones.
Gruszczy already solved your main problem, but here is some advice relevant to your code.
First, it’s easier to do a for
loop rather than keep track of iterations in a while
:
s = 0
for i in range(5):
s += int(raw_input('Enter a number: '))
Second, you can simplify it using the built-in sum
function:
s = sum(int(raw_input('Enter a number: ')) for i in range(5))
Third, both of the above will fail if the user enters invalid input. You should add a try
block to take care of this:
s = 0
for i in range(5):
try:
s += int(raw_input('Enter a number: '))
except ValueError:
print 'Invalid input. Counting as a zero.'
Or if you want to force 5 valid numbers:
round = 0
s = 0
while round < 5:
try:
s += int(raw_input('Enter a number: '))
except ValueError:
print 'Invalid input.'
else:
round += 1
Adding str or int by user_input & then printing the result –
Adding 2 or more no’s from users input
example from the abv link
'''Two numeric inputs, explicit sum'''
x = int(input("Enter an integer: "))
y = int(input("Enter another integer: "))
sum = x+y
sentence = 'The sum of {} and {} is {}.'.format(x, y, sum)
print(sentence)
x = 0
s = 0
while x < 5:
x += 1
s += (int(raw_input("Enter a number: ")))
print s
you could do this also
print ("enter input number : ")
input1 = int(raw_input())
sum1 = 0
for y in range(0,input1+1):
sum1 = sum1 + y
print ("the sum is " + str(sum1))
#Write a program to display the n terms of even number and their sum
input number of terms : 5
Expected Output : The even numbers are :2 4 6 8 10 The Sum of even Natural Number upto 5 terms : 30
As a practice exercise, I am trying to get five numbers from a user and return the sum of all five number using a while loop. I managed to gather the five numbers, but the sum is not provided by my code (I get a number, but it is always double the last number). I believe the issue is with my use of +=.
x = 0
while x < 5:
x += 1
s = (int(raw_input("Enter a number: ")))
s += s
print s
This should be better.
x = 0
s = 0
while x < 5:
x += 1
s += (int(raw_input("Enter a number: ")))
print s
You were putting one of the results on to the sum of all results and lost the previous ones.
Gruszczy already solved your main problem, but here is some advice relevant to your code.
First, it’s easier to do a for
loop rather than keep track of iterations in a while
:
s = 0
for i in range(5):
s += int(raw_input('Enter a number: '))
Second, you can simplify it using the built-in sum
function:
s = sum(int(raw_input('Enter a number: ')) for i in range(5))
Third, both of the above will fail if the user enters invalid input. You should add a try
block to take care of this:
s = 0
for i in range(5):
try:
s += int(raw_input('Enter a number: '))
except ValueError:
print 'Invalid input. Counting as a zero.'
Or if you want to force 5 valid numbers:
round = 0
s = 0
while round < 5:
try:
s += int(raw_input('Enter a number: '))
except ValueError:
print 'Invalid input.'
else:
round += 1
Adding str or int by user_input & then printing the result –
Adding 2 or more no’s from users input
example from the abv link
'''Two numeric inputs, explicit sum'''
x = int(input("Enter an integer: "))
y = int(input("Enter another integer: "))
sum = x+y
sentence = 'The sum of {} and {} is {}.'.format(x, y, sum)
print(sentence)
x = 0
s = 0
while x < 5:
x += 1
s += (int(raw_input("Enter a number: ")))
print s
you could do this also
print ("enter input number : ")
input1 = int(raw_input())
sum1 = 0
for y in range(0,input1+1):
sum1 = sum1 + y
print ("the sum is " + str(sum1))
#Write a program to display the n terms of even number and their sum
input number of terms : 5
Expected Output : The even numbers are :2 4 6 8 10 The Sum of even Natural Number upto 5 terms : 30