0

Python: problem with mediana in random number's list

Hi, I don't know what I did wrong. It's probably very simple, but I just can't find the answer, how sort the list and print mediana? I'd appreciate your help. #random numbers 0-20 import random for i in range(20): nums=random.randint(0,20) print(nums) #sort the list nums print (sorted(str(nums))) #mediana ?

11th Apr 2018, 10:53 PM
katar
katar - avatar
5 Answers
0
Something like this: print(sorted(nums)[len(nums)//2])
11th Apr 2018, 11:37 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
Unfortunately, it doesn't work. "TypeError: 'int' object is not iterable." Don't understand why. But thanks anyway.
12th Apr 2018, 3:43 PM
katar
katar - avatar
0
I think it should work. Works on my computer: >>> a = [1,3,4, 2,5] >>> print(sorted(a)[len(a)//2] 3
12th Apr 2018, 6:39 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
Of course, it works with a normal list, but not with my random number's list, or maybe I did something wrong in the first part? The problem is function sorted doesn't work in this code too :/
12th Apr 2018, 7:21 PM
katar
katar - avatar
0
nums = [] for i in range(20) nums.append(random.randint(0,20)
12th Apr 2018, 7:27 PM
Bartosz Pieszko
Bartosz Pieszko - avatar