-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
35 lines (30 loc) · 966 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def main():
#list = 1 -> TypeError
list = [7, 3, 0, 1, 5, 2, 1, "怪獣", 3.14, False]
mean, list = _mean(list)
print(mean, list)
def is_sorted(list):
for i in range(len(list)-1):
if list[i] > list[i+1]:
return False
return True
def _mean(list):
try:
for element in list:
if not type(element) in (int, float):
return None, list
while not is_sorted(list):
for i in range(len(list)-1):
if list[i] > list[i+1]:
tmp = list[i]
list[i] = list[i+1]
list[i+1] = tmp
if len(list) % 2 == 0:
mean = (list[int(len(list)/2)] + list[int(len(list)/2 - 1)])/2
else:
mean = list[int((len(list)-1)/2)]
return mean, list
except TypeError:
print("TypeError: variable 'list' must be list object")
if __name__ == "__main__":
main()