random7983405
random7983405

Reputation: 97

Python min, max, mean, median code function define error

Here's my code: Any help would be appreciated. Stuck on returning an error 'int' is not iterable and not sure where to go from there. Receiving error when running first part of my function at #minimum.

 #Minimum
min_val = min(input_list)
int(min_val)
return min_val

#Maximum
max_val = max(input_list)
int(max_val)
return max_val

#Mean
total = sum(input_list)
length = len(input_list)
for nums in [input_list]:
    mean_val = total / length
    float(mean_val)
    return mean_val

#Median
sorted(input_list)
med_val = sorted(lst)
lstLen = len(lst)
index = (lstLen - 1) // 2

if (lstLen % 2):
    return med_val[index]
else:
    return (med_val[index] + med_val[index + 1])/2.0

return min_val, max_val, mean_val, med_val

Upvotes: 2

Views: 21071

Answers (2)

Nick T
Nick T

Reputation: 26717

Python has a lot of baked-in functions and modules for doing common operations, i.e. basic stats (max, min, mean, median) via the statistics module.

import statistics

my_list = list(range(51))

print(min(my_list)) # 0
print(max(my_list)) # 50
print(statistics.mean(my_list)) # 25.0
print(statistics.median(my_list)) # 25

Upvotes: 3

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52163

There is no method named get_stats. Call stats instead.

min_val, max_val, mean_val, med_val = stats(my_list)

By the way, your code is totally wrong in terms of both indentation and logic. I'd recommend you to use max and min functions to get the maximum and minimum values in the list.

Upvotes: 1

Related Questions