Using Iterable and numeric as input in a function in python
I've written a mean function in python
def mean(*args):
numbers = args
sm=0
length = len(numbers)
if length>0:
for num in numbers:
sm = sm+num
return sm/length
else:
return 'FAIL'
which is working fine with numeric values. Now I want to modify it so that it also accepts iterables in the input.
Modified function:
def mean_new(*args):
numbers = args
sm=0
count=0
for num in numbers:
if iter(num):
data = list(num)
sm += sum(data)
count+=len(data)
else:
sm = sm+num
count+=1
return sm/count
mean_new function is not working and giving me an error 'int' object is not iterable. Could you please help me identify what I'm doing wrong in the mean_new function.
mean_new function should work with both numeric and iterable input.
python python-3.x function mean iterable
add a comment |
I've written a mean function in python
def mean(*args):
numbers = args
sm=0
length = len(numbers)
if length>0:
for num in numbers:
sm = sm+num
return sm/length
else:
return 'FAIL'
which is working fine with numeric values. Now I want to modify it so that it also accepts iterables in the input.
Modified function:
def mean_new(*args):
numbers = args
sm=0
count=0
for num in numbers:
if iter(num):
data = list(num)
sm += sum(data)
count+=len(data)
else:
sm = sm+num
count+=1
return sm/count
mean_new function is not working and giving me an error 'int' object is not iterable. Could you please help me identify what I'm doing wrong in the mean_new function.
mean_new function should work with both numeric and iterable input.
python python-3.x function mean iterable
What isiter(num)
here doing? Exactly what do you want to realize by accepting iterables? Can you provide some sample input?
– Willem Van Onsem
Nov 12 at 19:32
thats not how you check if something is iterable ...if isinstance(obj,collection.iterable)
or something like that
– Joran Beasley
Nov 12 at 19:32
2
as an aside,return 'FAIL'
is a bad design decision. Throw an error, don't return a magic string.
– juanpa.arrivillaga
Nov 12 at 19:34
It should work with the following inputs: mean_new(1,2,3,4) mean_new([1,1,1,2]) mean_new(1, (2,), 3, [4,6]))
– MSG
Nov 12 at 19:34
add a comment |
I've written a mean function in python
def mean(*args):
numbers = args
sm=0
length = len(numbers)
if length>0:
for num in numbers:
sm = sm+num
return sm/length
else:
return 'FAIL'
which is working fine with numeric values. Now I want to modify it so that it also accepts iterables in the input.
Modified function:
def mean_new(*args):
numbers = args
sm=0
count=0
for num in numbers:
if iter(num):
data = list(num)
sm += sum(data)
count+=len(data)
else:
sm = sm+num
count+=1
return sm/count
mean_new function is not working and giving me an error 'int' object is not iterable. Could you please help me identify what I'm doing wrong in the mean_new function.
mean_new function should work with both numeric and iterable input.
python python-3.x function mean iterable
I've written a mean function in python
def mean(*args):
numbers = args
sm=0
length = len(numbers)
if length>0:
for num in numbers:
sm = sm+num
return sm/length
else:
return 'FAIL'
which is working fine with numeric values. Now I want to modify it so that it also accepts iterables in the input.
Modified function:
def mean_new(*args):
numbers = args
sm=0
count=0
for num in numbers:
if iter(num):
data = list(num)
sm += sum(data)
count+=len(data)
else:
sm = sm+num
count+=1
return sm/count
mean_new function is not working and giving me an error 'int' object is not iterable. Could you please help me identify what I'm doing wrong in the mean_new function.
mean_new function should work with both numeric and iterable input.
python python-3.x function mean iterable
python python-3.x function mean iterable
asked Nov 12 at 19:30
MSG
103
103
What isiter(num)
here doing? Exactly what do you want to realize by accepting iterables? Can you provide some sample input?
– Willem Van Onsem
Nov 12 at 19:32
thats not how you check if something is iterable ...if isinstance(obj,collection.iterable)
or something like that
– Joran Beasley
Nov 12 at 19:32
2
as an aside,return 'FAIL'
is a bad design decision. Throw an error, don't return a magic string.
– juanpa.arrivillaga
Nov 12 at 19:34
It should work with the following inputs: mean_new(1,2,3,4) mean_new([1,1,1,2]) mean_new(1, (2,), 3, [4,6]))
– MSG
Nov 12 at 19:34
add a comment |
What isiter(num)
here doing? Exactly what do you want to realize by accepting iterables? Can you provide some sample input?
– Willem Van Onsem
Nov 12 at 19:32
thats not how you check if something is iterable ...if isinstance(obj,collection.iterable)
or something like that
– Joran Beasley
Nov 12 at 19:32
2
as an aside,return 'FAIL'
is a bad design decision. Throw an error, don't return a magic string.
– juanpa.arrivillaga
Nov 12 at 19:34
It should work with the following inputs: mean_new(1,2,3,4) mean_new([1,1,1,2]) mean_new(1, (2,), 3, [4,6]))
– MSG
Nov 12 at 19:34
What is
iter(num)
here doing? Exactly what do you want to realize by accepting iterables? Can you provide some sample input?– Willem Van Onsem
Nov 12 at 19:32
What is
iter(num)
here doing? Exactly what do you want to realize by accepting iterables? Can you provide some sample input?– Willem Van Onsem
Nov 12 at 19:32
thats not how you check if something is iterable ...
if isinstance(obj,collection.iterable)
or something like that– Joran Beasley
Nov 12 at 19:32
thats not how you check if something is iterable ...
if isinstance(obj,collection.iterable)
or something like that– Joran Beasley
Nov 12 at 19:32
2
2
as an aside,
return 'FAIL'
is a bad design decision. Throw an error, don't return a magic string.– juanpa.arrivillaga
Nov 12 at 19:34
as an aside,
return 'FAIL'
is a bad design decision. Throw an error, don't return a magic string.– juanpa.arrivillaga
Nov 12 at 19:34
It should work with the following inputs: mean_new(1,2,3,4) mean_new([1,1,1,2]) mean_new(1, (2,), 3, [4,6]))
– MSG
Nov 12 at 19:34
It should work with the following inputs: mean_new(1,2,3,4) mean_new([1,1,1,2]) mean_new(1, (2,), 3, [4,6]))
– MSG
Nov 12 at 19:34
add a comment |
1 Answer
1
active
oldest
votes
To check if an object is iterable, you can indeed use iter(object)
, but not as a condition in an if
statement, but in a try
block, since iter()
would raise a TypeError
exception if the object is not iterable. But then, since you are throwing away the iterator returned by iter()
and are instead using the list()
constructor to convert the iterable into a list, you can simply put the try
block around list()
instead:
for num in numbers:
try:
data = list(num)
sm += sum(data)
count += len(data)
except TypeError:
sm = sm + num
count += 1
1
It worked. Thanks.:)
– MSG
Nov 12 at 19:53
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%2f53268878%2fusing-iterable-and-numeric-as-input-in-a-function-in-python%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
To check if an object is iterable, you can indeed use iter(object)
, but not as a condition in an if
statement, but in a try
block, since iter()
would raise a TypeError
exception if the object is not iterable. But then, since you are throwing away the iterator returned by iter()
and are instead using the list()
constructor to convert the iterable into a list, you can simply put the try
block around list()
instead:
for num in numbers:
try:
data = list(num)
sm += sum(data)
count += len(data)
except TypeError:
sm = sm + num
count += 1
1
It worked. Thanks.:)
– MSG
Nov 12 at 19:53
add a comment |
To check if an object is iterable, you can indeed use iter(object)
, but not as a condition in an if
statement, but in a try
block, since iter()
would raise a TypeError
exception if the object is not iterable. But then, since you are throwing away the iterator returned by iter()
and are instead using the list()
constructor to convert the iterable into a list, you can simply put the try
block around list()
instead:
for num in numbers:
try:
data = list(num)
sm += sum(data)
count += len(data)
except TypeError:
sm = sm + num
count += 1
1
It worked. Thanks.:)
– MSG
Nov 12 at 19:53
add a comment |
To check if an object is iterable, you can indeed use iter(object)
, but not as a condition in an if
statement, but in a try
block, since iter()
would raise a TypeError
exception if the object is not iterable. But then, since you are throwing away the iterator returned by iter()
and are instead using the list()
constructor to convert the iterable into a list, you can simply put the try
block around list()
instead:
for num in numbers:
try:
data = list(num)
sm += sum(data)
count += len(data)
except TypeError:
sm = sm + num
count += 1
To check if an object is iterable, you can indeed use iter(object)
, but not as a condition in an if
statement, but in a try
block, since iter()
would raise a TypeError
exception if the object is not iterable. But then, since you are throwing away the iterator returned by iter()
and are instead using the list()
constructor to convert the iterable into a list, you can simply put the try
block around list()
instead:
for num in numbers:
try:
data = list(num)
sm += sum(data)
count += len(data)
except TypeError:
sm = sm + num
count += 1
answered Nov 12 at 19:42
blhsing
28.5k41336
28.5k41336
1
It worked. Thanks.:)
– MSG
Nov 12 at 19:53
add a comment |
1
It worked. Thanks.:)
– MSG
Nov 12 at 19:53
1
1
It worked. Thanks.:)
– MSG
Nov 12 at 19:53
It worked. Thanks.:)
– MSG
Nov 12 at 19:53
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53268878%2fusing-iterable-and-numeric-as-input-in-a-function-in-python%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
What is
iter(num)
here doing? Exactly what do you want to realize by accepting iterables? Can you provide some sample input?– Willem Van Onsem
Nov 12 at 19:32
thats not how you check if something is iterable ...
if isinstance(obj,collection.iterable)
or something like that– Joran Beasley
Nov 12 at 19:32
2
as an aside,
return 'FAIL'
is a bad design decision. Throw an error, don't return a magic string.– juanpa.arrivillaga
Nov 12 at 19:34
It should work with the following inputs: mean_new(1,2,3,4) mean_new([1,1,1,2]) mean_new(1, (2,), 3, [4,6]))
– MSG
Nov 12 at 19:34