How to return the result for arithmetic operations with lists using functions in Python [duplicate]
This question already has an answer here:
error :list indices must be integers, not float for median
2 answers
I'm trying to compute the mean, median and mode of a list of integers. However, the error I'm getting (from what I've understood) states that I cannot return a floating point answer. I don't understand why this is the case for lists. All I'm doing is a basic arithmetic operation for both mean and median (using the statistics framework for mode). The output should be just 1 single answer.
This is the code I have:
from statistics import mode
listOfNumbers = [1, 2, 6, 7, 8, 9, 3, 4, 5, 10, 10]
listOfNumbers.sort()
length = len(listOfNumbers)
def median(numbers):
if((len(list)) % 2 == 0):
median = (listOfNumbers[(length)/2] + listOfNumbers[(length)/2-1]) / 2
else:
median = listOfNumbers[(length-1)/2]
return
def meanMedianMode(numbers):
# TODO your code here!
meanOfNumbers = (sum(numbers))/(len(numbers))
medianOfNumbers = median(numbers)
modeOfNumbers = mode(numbers)
print("The mean of the numbers is: " + str(meanOfNumbers))
print("The median of the numbers is: " + str(medianOfNumbers))
print("The mode of the numbers is: " + str(modeOfNumbers))
meanMedianMode(listOfNumbers)
And this is my output:
TypeError Traceback (most recent call last)
<ipython-input-27-03d94981e27d> in <module>()
21 print("The mode of the numbers is: " + str(modeOfNumbers))
22
---> 23 meanMedianMode(listOfNumbers)
24
<ipython-input-27-03d94981e27d> in meanMedianMode(numbers)
15 # TODO your code here!
16 meanOfNumbers = (sum(numbers))/(len(numbers))
---> 17 medianOfNumbers = median(numbers)
18 modeOfNumbers = mode(numbers)
19 print("The mean of the numbers is: " + str(meanOfNumbers))
<ipython-input-27-03d94981e27d> in median(numbers)
9 median = (listOfNumbers[(length)/2] + listOfNumbers[(length)/2-1]) / 2
10 else:
---> 11 median = listOfNumbers[(length-1)/2]
12 return
13
TypeError: list indices must be integers or slices, not float
python types statistics
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 12:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
error :list indices must be integers, not float for median
2 answers
I'm trying to compute the mean, median and mode of a list of integers. However, the error I'm getting (from what I've understood) states that I cannot return a floating point answer. I don't understand why this is the case for lists. All I'm doing is a basic arithmetic operation for both mean and median (using the statistics framework for mode). The output should be just 1 single answer.
This is the code I have:
from statistics import mode
listOfNumbers = [1, 2, 6, 7, 8, 9, 3, 4, 5, 10, 10]
listOfNumbers.sort()
length = len(listOfNumbers)
def median(numbers):
if((len(list)) % 2 == 0):
median = (listOfNumbers[(length)/2] + listOfNumbers[(length)/2-1]) / 2
else:
median = listOfNumbers[(length-1)/2]
return
def meanMedianMode(numbers):
# TODO your code here!
meanOfNumbers = (sum(numbers))/(len(numbers))
medianOfNumbers = median(numbers)
modeOfNumbers = mode(numbers)
print("The mean of the numbers is: " + str(meanOfNumbers))
print("The median of the numbers is: " + str(medianOfNumbers))
print("The mode of the numbers is: " + str(modeOfNumbers))
meanMedianMode(listOfNumbers)
And this is my output:
TypeError Traceback (most recent call last)
<ipython-input-27-03d94981e27d> in <module>()
21 print("The mode of the numbers is: " + str(modeOfNumbers))
22
---> 23 meanMedianMode(listOfNumbers)
24
<ipython-input-27-03d94981e27d> in meanMedianMode(numbers)
15 # TODO your code here!
16 meanOfNumbers = (sum(numbers))/(len(numbers))
---> 17 medianOfNumbers = median(numbers)
18 modeOfNumbers = mode(numbers)
19 print("The mean of the numbers is: " + str(meanOfNumbers))
<ipython-input-27-03d94981e27d> in median(numbers)
9 median = (listOfNumbers[(length)/2] + listOfNumbers[(length)/2-1]) / 2
10 else:
---> 11 median = listOfNumbers[(length-1)/2]
12 return
13
TypeError: list indices must be integers or slices, not float
python types statistics
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 12:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
error :list indices must be integers, not float for median
2 answers
I'm trying to compute the mean, median and mode of a list of integers. However, the error I'm getting (from what I've understood) states that I cannot return a floating point answer. I don't understand why this is the case for lists. All I'm doing is a basic arithmetic operation for both mean and median (using the statistics framework for mode). The output should be just 1 single answer.
This is the code I have:
from statistics import mode
listOfNumbers = [1, 2, 6, 7, 8, 9, 3, 4, 5, 10, 10]
listOfNumbers.sort()
length = len(listOfNumbers)
def median(numbers):
if((len(list)) % 2 == 0):
median = (listOfNumbers[(length)/2] + listOfNumbers[(length)/2-1]) / 2
else:
median = listOfNumbers[(length-1)/2]
return
def meanMedianMode(numbers):
# TODO your code here!
meanOfNumbers = (sum(numbers))/(len(numbers))
medianOfNumbers = median(numbers)
modeOfNumbers = mode(numbers)
print("The mean of the numbers is: " + str(meanOfNumbers))
print("The median of the numbers is: " + str(medianOfNumbers))
print("The mode of the numbers is: " + str(modeOfNumbers))
meanMedianMode(listOfNumbers)
And this is my output:
TypeError Traceback (most recent call last)
<ipython-input-27-03d94981e27d> in <module>()
21 print("The mode of the numbers is: " + str(modeOfNumbers))
22
---> 23 meanMedianMode(listOfNumbers)
24
<ipython-input-27-03d94981e27d> in meanMedianMode(numbers)
15 # TODO your code here!
16 meanOfNumbers = (sum(numbers))/(len(numbers))
---> 17 medianOfNumbers = median(numbers)
18 modeOfNumbers = mode(numbers)
19 print("The mean of the numbers is: " + str(meanOfNumbers))
<ipython-input-27-03d94981e27d> in median(numbers)
9 median = (listOfNumbers[(length)/2] + listOfNumbers[(length)/2-1]) / 2
10 else:
---> 11 median = listOfNumbers[(length-1)/2]
12 return
13
TypeError: list indices must be integers or slices, not float
python types statistics
This question already has an answer here:
error :list indices must be integers, not float for median
2 answers
I'm trying to compute the mean, median and mode of a list of integers. However, the error I'm getting (from what I've understood) states that I cannot return a floating point answer. I don't understand why this is the case for lists. All I'm doing is a basic arithmetic operation for both mean and median (using the statistics framework for mode). The output should be just 1 single answer.
This is the code I have:
from statistics import mode
listOfNumbers = [1, 2, 6, 7, 8, 9, 3, 4, 5, 10, 10]
listOfNumbers.sort()
length = len(listOfNumbers)
def median(numbers):
if((len(list)) % 2 == 0):
median = (listOfNumbers[(length)/2] + listOfNumbers[(length)/2-1]) / 2
else:
median = listOfNumbers[(length-1)/2]
return
def meanMedianMode(numbers):
# TODO your code here!
meanOfNumbers = (sum(numbers))/(len(numbers))
medianOfNumbers = median(numbers)
modeOfNumbers = mode(numbers)
print("The mean of the numbers is: " + str(meanOfNumbers))
print("The median of the numbers is: " + str(medianOfNumbers))
print("The mode of the numbers is: " + str(modeOfNumbers))
meanMedianMode(listOfNumbers)
And this is my output:
TypeError Traceback (most recent call last)
<ipython-input-27-03d94981e27d> in <module>()
21 print("The mode of the numbers is: " + str(modeOfNumbers))
22
---> 23 meanMedianMode(listOfNumbers)
24
<ipython-input-27-03d94981e27d> in meanMedianMode(numbers)
15 # TODO your code here!
16 meanOfNumbers = (sum(numbers))/(len(numbers))
---> 17 medianOfNumbers = median(numbers)
18 modeOfNumbers = mode(numbers)
19 print("The mean of the numbers is: " + str(meanOfNumbers))
<ipython-input-27-03d94981e27d> in median(numbers)
9 median = (listOfNumbers[(length)/2] + listOfNumbers[(length)/2-1]) / 2
10 else:
---> 11 median = listOfNumbers[(length-1)/2]
12 return
13
TypeError: list indices must be integers or slices, not float
This question already has an answer here:
error :list indices must be integers, not float for median
2 answers
python types statistics
python types statistics
edited Jan 2 at 12:40


KbiR
2,39111438
2,39111438
asked Jan 2 at 2:07
user10851318
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 12:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 12:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The problem lies in this line:
median = listOfNumbers[(length-1)/2]
In python 3, (length-1)/2
is a float
, not an int
, hence the error list indices must be integers or slices, not float
Easiest fix is to cast the result back into an int
:
...
median = listOfNumbers[int((length - 1) / 2)]
...
In fact I would refactor your entire median()
function like this, as you're mixing local and global variables, and not returning anything:
def median(numbers):
length = len(numbers)
if length % 2 == 0:
return (listOfNumbers[int(length / 2)] + listOfNumbers[int(length / 2) - 1]) / 2
else:
return listOfNumbers[int((length - 1) / 2)]
He needs to fix the "return" lines as well
– john gonidelis
Jan 2 at 2:36
1
Thanks, @Danielle. I've accepted and upvoted your answer. If you think that this was a well asked question, could you give me an upvote as well?
– user10851318
Jan 2 at 5:50
add a comment |
Okay the fix:
median_value = numbers[int((length - 1 )/2)]
return median_value
A few of things here...
numbers[x] means the the xth elements in numbers so a value like 5.5 doesn't point to anything.
6.0 is still a decimal number (or float), we want an integer so use int() so that this is now 6.
Ideally you want the median function to return these values after the list has been passed through (hence return median_value)
You would also have to do the same thing for the other part of the if statement (not executed in this run).
median_value = (numbers[int((length - 1)/2))] + numbers[int((length + 1)/2)]) / 2
return median_value
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem lies in this line:
median = listOfNumbers[(length-1)/2]
In python 3, (length-1)/2
is a float
, not an int
, hence the error list indices must be integers or slices, not float
Easiest fix is to cast the result back into an int
:
...
median = listOfNumbers[int((length - 1) / 2)]
...
In fact I would refactor your entire median()
function like this, as you're mixing local and global variables, and not returning anything:
def median(numbers):
length = len(numbers)
if length % 2 == 0:
return (listOfNumbers[int(length / 2)] + listOfNumbers[int(length / 2) - 1]) / 2
else:
return listOfNumbers[int((length - 1) / 2)]
He needs to fix the "return" lines as well
– john gonidelis
Jan 2 at 2:36
1
Thanks, @Danielle. I've accepted and upvoted your answer. If you think that this was a well asked question, could you give me an upvote as well?
– user10851318
Jan 2 at 5:50
add a comment |
The problem lies in this line:
median = listOfNumbers[(length-1)/2]
In python 3, (length-1)/2
is a float
, not an int
, hence the error list indices must be integers or slices, not float
Easiest fix is to cast the result back into an int
:
...
median = listOfNumbers[int((length - 1) / 2)]
...
In fact I would refactor your entire median()
function like this, as you're mixing local and global variables, and not returning anything:
def median(numbers):
length = len(numbers)
if length % 2 == 0:
return (listOfNumbers[int(length / 2)] + listOfNumbers[int(length / 2) - 1]) / 2
else:
return listOfNumbers[int((length - 1) / 2)]
He needs to fix the "return" lines as well
– john gonidelis
Jan 2 at 2:36
1
Thanks, @Danielle. I've accepted and upvoted your answer. If you think that this was a well asked question, could you give me an upvote as well?
– user10851318
Jan 2 at 5:50
add a comment |
The problem lies in this line:
median = listOfNumbers[(length-1)/2]
In python 3, (length-1)/2
is a float
, not an int
, hence the error list indices must be integers or slices, not float
Easiest fix is to cast the result back into an int
:
...
median = listOfNumbers[int((length - 1) / 2)]
...
In fact I would refactor your entire median()
function like this, as you're mixing local and global variables, and not returning anything:
def median(numbers):
length = len(numbers)
if length % 2 == 0:
return (listOfNumbers[int(length / 2)] + listOfNumbers[int(length / 2) - 1]) / 2
else:
return listOfNumbers[int((length - 1) / 2)]
The problem lies in this line:
median = listOfNumbers[(length-1)/2]
In python 3, (length-1)/2
is a float
, not an int
, hence the error list indices must be integers or slices, not float
Easiest fix is to cast the result back into an int
:
...
median = listOfNumbers[int((length - 1) / 2)]
...
In fact I would refactor your entire median()
function like this, as you're mixing local and global variables, and not returning anything:
def median(numbers):
length = len(numbers)
if length % 2 == 0:
return (listOfNumbers[int(length / 2)] + listOfNumbers[int(length / 2) - 1]) / 2
else:
return listOfNumbers[int((length - 1) / 2)]
edited Jan 2 at 2:33
answered Jan 2 at 2:28


Danielle M.Danielle M.
1,9251523
1,9251523
He needs to fix the "return" lines as well
– john gonidelis
Jan 2 at 2:36
1
Thanks, @Danielle. I've accepted and upvoted your answer. If you think that this was a well asked question, could you give me an upvote as well?
– user10851318
Jan 2 at 5:50
add a comment |
He needs to fix the "return" lines as well
– john gonidelis
Jan 2 at 2:36
1
Thanks, @Danielle. I've accepted and upvoted your answer. If you think that this was a well asked question, could you give me an upvote as well?
– user10851318
Jan 2 at 5:50
He needs to fix the "return" lines as well
– john gonidelis
Jan 2 at 2:36
He needs to fix the "return" lines as well
– john gonidelis
Jan 2 at 2:36
1
1
Thanks, @Danielle. I've accepted and upvoted your answer. If you think that this was a well asked question, could you give me an upvote as well?
– user10851318
Jan 2 at 5:50
Thanks, @Danielle. I've accepted and upvoted your answer. If you think that this was a well asked question, could you give me an upvote as well?
– user10851318
Jan 2 at 5:50
add a comment |
Okay the fix:
median_value = numbers[int((length - 1 )/2)]
return median_value
A few of things here...
numbers[x] means the the xth elements in numbers so a value like 5.5 doesn't point to anything.
6.0 is still a decimal number (or float), we want an integer so use int() so that this is now 6.
Ideally you want the median function to return these values after the list has been passed through (hence return median_value)
You would also have to do the same thing for the other part of the if statement (not executed in this run).
median_value = (numbers[int((length - 1)/2))] + numbers[int((length + 1)/2)]) / 2
return median_value
add a comment |
Okay the fix:
median_value = numbers[int((length - 1 )/2)]
return median_value
A few of things here...
numbers[x] means the the xth elements in numbers so a value like 5.5 doesn't point to anything.
6.0 is still a decimal number (or float), we want an integer so use int() so that this is now 6.
Ideally you want the median function to return these values after the list has been passed through (hence return median_value)
You would also have to do the same thing for the other part of the if statement (not executed in this run).
median_value = (numbers[int((length - 1)/2))] + numbers[int((length + 1)/2)]) / 2
return median_value
add a comment |
Okay the fix:
median_value = numbers[int((length - 1 )/2)]
return median_value
A few of things here...
numbers[x] means the the xth elements in numbers so a value like 5.5 doesn't point to anything.
6.0 is still a decimal number (or float), we want an integer so use int() so that this is now 6.
Ideally you want the median function to return these values after the list has been passed through (hence return median_value)
You would also have to do the same thing for the other part of the if statement (not executed in this run).
median_value = (numbers[int((length - 1)/2))] + numbers[int((length + 1)/2)]) / 2
return median_value
Okay the fix:
median_value = numbers[int((length - 1 )/2)]
return median_value
A few of things here...
numbers[x] means the the xth elements in numbers so a value like 5.5 doesn't point to anything.
6.0 is still a decimal number (or float), we want an integer so use int() so that this is now 6.
Ideally you want the median function to return these values after the list has been passed through (hence return median_value)
You would also have to do the same thing for the other part of the if statement (not executed in this run).
median_value = (numbers[int((length - 1)/2))] + numbers[int((length + 1)/2)]) / 2
return median_value
edited Jan 2 at 3:30
answered Jan 2 at 3:21


Adam DadAdam Dad
114
114
add a comment |
add a comment |