How can is sum up an input range of numbers?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Dear stackoverflow community!
I just started learning python and want to figure out how to write following program:`
number = int(input('Enter ten numbers:'))
for i in range(1, 10):
while True:
try:
number = int(input("Enter another number: "))
break
except:
print("This is a string")
for i in range(1, 10):
res = 0
res += int(input())
print('The sum of these 10 numbers is:', res)
I want the user to input 10 numbers and during the process I want to check if the numbers are actually integers. So the number input works, it checks if its an integer, but then how can make this work at the same time? (To sum up the 10 integers that I got as input) :
for i in range(1, 10):
res = 0
res += int(input())
print('The sum of these 10 numbers is:', res)
So basically i want two conditions for those 10 numbers that I got as Input.
Thanks for your help.
python sum range
add a comment |
Dear stackoverflow community!
I just started learning python and want to figure out how to write following program:`
number = int(input('Enter ten numbers:'))
for i in range(1, 10):
while True:
try:
number = int(input("Enter another number: "))
break
except:
print("This is a string")
for i in range(1, 10):
res = 0
res += int(input())
print('The sum of these 10 numbers is:', res)
I want the user to input 10 numbers and during the process I want to check if the numbers are actually integers. So the number input works, it checks if its an integer, but then how can make this work at the same time? (To sum up the 10 integers that I got as input) :
for i in range(1, 10):
res = 0
res += int(input())
print('The sum of these 10 numbers is:', res)
So basically i want two conditions for those 10 numbers that I got as Input.
Thanks for your help.
python sum range
3
you need to defineres = 0
before the loop
– Chris_Rands
Nov 16 '18 at 12:24
You only need to loop once, not twice. Are you sure you know whatinput
does? You seem to treat it as a storage variable of some kind.
– usr2564301
Nov 16 '18 at 12:25
1
range(1, 10)
are only 9 values, since the "end" argument is not inclusive. Also you overwrite the value of your single variablenumber
, you don't store the numbers anywhere. You also don't handle the error when the first input is not a number, since it is outside oftry ... except
.
– Christian König
Nov 16 '18 at 12:26
thanks for the input guys; I did know that looping twice doesnt work, I just wanted to visualize that I want to do 2 things with this loop basically x) @Chris_Rands; can you maybe elaborate?
– Proiegomena
Nov 16 '18 at 13:47
add a comment |
Dear stackoverflow community!
I just started learning python and want to figure out how to write following program:`
number = int(input('Enter ten numbers:'))
for i in range(1, 10):
while True:
try:
number = int(input("Enter another number: "))
break
except:
print("This is a string")
for i in range(1, 10):
res = 0
res += int(input())
print('The sum of these 10 numbers is:', res)
I want the user to input 10 numbers and during the process I want to check if the numbers are actually integers. So the number input works, it checks if its an integer, but then how can make this work at the same time? (To sum up the 10 integers that I got as input) :
for i in range(1, 10):
res = 0
res += int(input())
print('The sum of these 10 numbers is:', res)
So basically i want two conditions for those 10 numbers that I got as Input.
Thanks for your help.
python sum range
Dear stackoverflow community!
I just started learning python and want to figure out how to write following program:`
number = int(input('Enter ten numbers:'))
for i in range(1, 10):
while True:
try:
number = int(input("Enter another number: "))
break
except:
print("This is a string")
for i in range(1, 10):
res = 0
res += int(input())
print('The sum of these 10 numbers is:', res)
I want the user to input 10 numbers and during the process I want to check if the numbers are actually integers. So the number input works, it checks if its an integer, but then how can make this work at the same time? (To sum up the 10 integers that I got as input) :
for i in range(1, 10):
res = 0
res += int(input())
print('The sum of these 10 numbers is:', res)
So basically i want two conditions for those 10 numbers that I got as Input.
Thanks for your help.
python sum range
python sum range
asked Nov 16 '18 at 12:22
ProiegomenaProiegomena
62
62
3
you need to defineres = 0
before the loop
– Chris_Rands
Nov 16 '18 at 12:24
You only need to loop once, not twice. Are you sure you know whatinput
does? You seem to treat it as a storage variable of some kind.
– usr2564301
Nov 16 '18 at 12:25
1
range(1, 10)
are only 9 values, since the "end" argument is not inclusive. Also you overwrite the value of your single variablenumber
, you don't store the numbers anywhere. You also don't handle the error when the first input is not a number, since it is outside oftry ... except
.
– Christian König
Nov 16 '18 at 12:26
thanks for the input guys; I did know that looping twice doesnt work, I just wanted to visualize that I want to do 2 things with this loop basically x) @Chris_Rands; can you maybe elaborate?
– Proiegomena
Nov 16 '18 at 13:47
add a comment |
3
you need to defineres = 0
before the loop
– Chris_Rands
Nov 16 '18 at 12:24
You only need to loop once, not twice. Are you sure you know whatinput
does? You seem to treat it as a storage variable of some kind.
– usr2564301
Nov 16 '18 at 12:25
1
range(1, 10)
are only 9 values, since the "end" argument is not inclusive. Also you overwrite the value of your single variablenumber
, you don't store the numbers anywhere. You also don't handle the error when the first input is not a number, since it is outside oftry ... except
.
– Christian König
Nov 16 '18 at 12:26
thanks for the input guys; I did know that looping twice doesnt work, I just wanted to visualize that I want to do 2 things with this loop basically x) @Chris_Rands; can you maybe elaborate?
– Proiegomena
Nov 16 '18 at 13:47
3
3
you need to define
res = 0
before the loop– Chris_Rands
Nov 16 '18 at 12:24
you need to define
res = 0
before the loop– Chris_Rands
Nov 16 '18 at 12:24
You only need to loop once, not twice. Are you sure you know what
input
does? You seem to treat it as a storage variable of some kind.– usr2564301
Nov 16 '18 at 12:25
You only need to loop once, not twice. Are you sure you know what
input
does? You seem to treat it as a storage variable of some kind.– usr2564301
Nov 16 '18 at 12:25
1
1
range(1, 10)
are only 9 values, since the "end" argument is not inclusive. Also you overwrite the value of your single variable number
, you don't store the numbers anywhere. You also don't handle the error when the first input is not a number, since it is outside of try ... except
.– Christian König
Nov 16 '18 at 12:26
range(1, 10)
are only 9 values, since the "end" argument is not inclusive. Also you overwrite the value of your single variable number
, you don't store the numbers anywhere. You also don't handle the error when the first input is not a number, since it is outside of try ... except
.– Christian König
Nov 16 '18 at 12:26
thanks for the input guys; I did know that looping twice doesnt work, I just wanted to visualize that I want to do 2 things with this loop basically x) @Chris_Rands; can you maybe elaborate?
– Proiegomena
Nov 16 '18 at 13:47
thanks for the input guys; I did know that looping twice doesnt work, I just wanted to visualize that I want to do 2 things with this loop basically x) @Chris_Rands; can you maybe elaborate?
– Proiegomena
Nov 16 '18 at 13:47
add a comment |
1 Answer
1
active
oldest
votes
You are simply checking the user input, not storing it somewhere. Use this instead:
numbers =
while len(numbers) != 10:
try:
numbers.append(int(input("Enter another number: ")))
except ValueError:
print("This is not an integer!")
res = sum(numbers)
print('The sum of these 10 numbers is: {}'.format(res))
sum
is of course the way to go, but you might point out the error in the OP's loop based summing.
– schwobaseggl
Nov 16 '18 at 12:36
oh yea, this approach with list makes way more sense. Thanks! I suppose I do know that I cant loop twice with for i in range(); however I did hope there's an easy solution how to add summing the parts to the "check if its an integer" loop.
– Proiegomena
Nov 16 '18 at 13:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337852%2fhow-can-is-sum-up-an-input-range-of-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are simply checking the user input, not storing it somewhere. Use this instead:
numbers =
while len(numbers) != 10:
try:
numbers.append(int(input("Enter another number: ")))
except ValueError:
print("This is not an integer!")
res = sum(numbers)
print('The sum of these 10 numbers is: {}'.format(res))
sum
is of course the way to go, but you might point out the error in the OP's loop based summing.
– schwobaseggl
Nov 16 '18 at 12:36
oh yea, this approach with list makes way more sense. Thanks! I suppose I do know that I cant loop twice with for i in range(); however I did hope there's an easy solution how to add summing the parts to the "check if its an integer" loop.
– Proiegomena
Nov 16 '18 at 13:44
add a comment |
You are simply checking the user input, not storing it somewhere. Use this instead:
numbers =
while len(numbers) != 10:
try:
numbers.append(int(input("Enter another number: ")))
except ValueError:
print("This is not an integer!")
res = sum(numbers)
print('The sum of these 10 numbers is: {}'.format(res))
sum
is of course the way to go, but you might point out the error in the OP's loop based summing.
– schwobaseggl
Nov 16 '18 at 12:36
oh yea, this approach with list makes way more sense. Thanks! I suppose I do know that I cant loop twice with for i in range(); however I did hope there's an easy solution how to add summing the parts to the "check if its an integer" loop.
– Proiegomena
Nov 16 '18 at 13:44
add a comment |
You are simply checking the user input, not storing it somewhere. Use this instead:
numbers =
while len(numbers) != 10:
try:
numbers.append(int(input("Enter another number: ")))
except ValueError:
print("This is not an integer!")
res = sum(numbers)
print('The sum of these 10 numbers is: {}'.format(res))
You are simply checking the user input, not storing it somewhere. Use this instead:
numbers =
while len(numbers) != 10:
try:
numbers.append(int(input("Enter another number: ")))
except ValueError:
print("This is not an integer!")
res = sum(numbers)
print('The sum of these 10 numbers is: {}'.format(res))
answered Nov 16 '18 at 12:26
Ev. KounisEv. Kounis
11.4k21751
11.4k21751
sum
is of course the way to go, but you might point out the error in the OP's loop based summing.
– schwobaseggl
Nov 16 '18 at 12:36
oh yea, this approach with list makes way more sense. Thanks! I suppose I do know that I cant loop twice with for i in range(); however I did hope there's an easy solution how to add summing the parts to the "check if its an integer" loop.
– Proiegomena
Nov 16 '18 at 13:44
add a comment |
sum
is of course the way to go, but you might point out the error in the OP's loop based summing.
– schwobaseggl
Nov 16 '18 at 12:36
oh yea, this approach with list makes way more sense. Thanks! I suppose I do know that I cant loop twice with for i in range(); however I did hope there's an easy solution how to add summing the parts to the "check if its an integer" loop.
– Proiegomena
Nov 16 '18 at 13:44
sum
is of course the way to go, but you might point out the error in the OP's loop based summing.– schwobaseggl
Nov 16 '18 at 12:36
sum
is of course the way to go, but you might point out the error in the OP's loop based summing.– schwobaseggl
Nov 16 '18 at 12:36
oh yea, this approach with list makes way more sense. Thanks! I suppose I do know that I cant loop twice with for i in range(); however I did hope there's an easy solution how to add summing the parts to the "check if its an integer" loop.
– Proiegomena
Nov 16 '18 at 13:44
oh yea, this approach with list makes way more sense. Thanks! I suppose I do know that I cant loop twice with for i in range(); however I did hope there's an easy solution how to add summing the parts to the "check if its an integer" loop.
– Proiegomena
Nov 16 '18 at 13:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337852%2fhow-can-is-sum-up-an-input-range-of-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
you need to define
res = 0
before the loop– Chris_Rands
Nov 16 '18 at 12:24
You only need to loop once, not twice. Are you sure you know what
input
does? You seem to treat it as a storage variable of some kind.– usr2564301
Nov 16 '18 at 12:25
1
range(1, 10)
are only 9 values, since the "end" argument is not inclusive. Also you overwrite the value of your single variablenumber
, you don't store the numbers anywhere. You also don't handle the error when the first input is not a number, since it is outside oftry ... except
.– Christian König
Nov 16 '18 at 12:26
thanks for the input guys; I did know that looping twice doesnt work, I just wanted to visualize that I want to do 2 things with this loop basically x) @Chris_Rands; can you maybe elaborate?
– Proiegomena
Nov 16 '18 at 13:47