Respuesta :
Answer:
In Python:
def median(mylist):
mylist.sort()
if len(mylist) % 2 == 1:
midIndex = int((len(mylist) +1)/ 2)
print("Median: "+str(mylist[midIndex-1]))
else:
midIndex = int((len(mylist))/ 2)
Mid = (mylist[midIndex] + mylist[midIndex-1] )/2
print("Median: "+str(Mid))
def mean(mylist):
isum = 0
for i in range(len(mylist)):
isum += mylist[i]
ave = isum/len(mylist)
print("Mean: "+str(ave))
def mode(mylist):
print("Mode: "+str(max(set(mylist), key=mylist.count)))
Explanation:
Your program is a bit difficult to read and trace. So, I rewrite the program.
This defines the median function
def median(mylist):
This sorts the list
mylist.sort()
This checks if the list count is odd
if len(mylist) % 2 == 1:
If yes, it calculates the mid index
midIndex = int((len(mylist) +1)/ 2)
And prints the median
print("Median: "+str(mylist[midIndex-1]))
else:
If otherwise, it calculates the mid indices
midIndex = int((len(mylist))/ 2)
Mid = (mylist[midIndex] + mylist[midIndex-1] )/2
And prints the median
print("Median: "+str(Mid))
This defines the mean function
def mean(mylist):
This initializes sum to 0
isum = 0
This iterates through the list
for i in range(len(mylist)):
This calculates the sum of items in the list
isum += mylist[i]
This calculates the mean
ave = isum/len(mylist)
This prints the mean
print("Mean: "+str(ave))
This defines the mode
def mode(mylist):
This calculates and prints the mode using the max function
print("Mode: "+str(max(set(mylist), key=mylist.count)))