Форум сайта python.su
0
sergeekСделал так. Выдает ошибку.
enter a number or Enter to finsh:8 enter a number or Enter to finsh:5 enter a number or Enter to finsh:6 enter a number or Enter to finsh:455 enter a number or Enter to finsh:454 enter a number or Enter to finsh:7 enter a number or Enter to finsh:9 enter a number or Enter to finsh:3 enter a number or Enter to finsh:2 enter a number or Enter to finsh:4 enter a number or Enter to finsh:6886 enter a number or Enter to finsh:55 enter a number or Enter to finsh:46 enter a number or Enter to finsh:46 enter a number or Enter to finsh:6 enter a number or Enter to finsh:46 enter a number or Enter to finsh: List = [8, 5, 6, 455, 454, 7, 9, 3, 2, 4, 6886, 55, 46, 46, 6, 46] count = 16 Total = 8038 Seredne = 502.375 Min = 2 Max = 6886 [2, 3, 4, 5, 6, 6, 7, 8, 9, 46, 46, 46, 55, 454, 455, 6886] Traceback (most recent call last): File "sum1.py", line 45, in <module> List_2=med(List) File "sum1.py", line 42, in med return (max(lst[:hf]) + min(lst[hf:])) / 2 TypeError: 'int' object is not callable ***Repl Closed***
count=0 List=[] total=0 while True: a= input("enter a number or Enter to finsh:") if a: try: number=int(a) except ValueError as err: print(err) continue total+=number count+=1 list.append(List,number) Li=len(List) else: break i=0 min=List[i] max=List[i] while i<len(List): if List[i]<min: min=List[i] if List[i]>max: max=List[i] i+=1 if count: print("List = ",List) print("count = ",count,"Total = ",total,"Seredne = ", total/count, "Min = ", min, "Max = ", max) for I in range(count): idxMin=I for J in range(I+1,count): if List[J]<List[idxMin]: idxMin=J temp=List[idxMin] List[idxMin]=List[I] List[I]=temp print(List) def med(lst): hf = len(lst) // 2 if hf*2 == len(lst): return (max(lst[:hf]) + min(lst[hf:])) / 2 else: return lst[hf] List_2=med(List) print(List_2)
Офлайн
857
Shaman
Возле ValueError и list.append, недалеко, было про help(list).
Ageraон имел в виду, что ты “неправильно” присоединяешь к списку, потому что надо вызывать метод у списка и передавать один аргумент ;)
Непонят что Shaman имеете ввиду.
>>> lst = [] >>> list.append(lst, 1) >>> list.append(lst, 2) >>> lst [1, 2] >>>
Ageraты не можешь пользоваться функциями max и min, потому что перекрыл их имена, использовав под переменныеreturn (max(lst[:hf]) + min(lst[hf:])) / 2
TypeError: 'int' object is not callable
Отредактировано py.user.next (Март 24, 2013 02:32:09)
Офлайн
43
def med(lst): lst = sorted(lst) hf = len(lst) // 2 if hf*2 == len(lst): return (lst[:hf][-1] + lst[hf:][0]) / 2.0 else: return lst[hf]
Офлайн
0
sergeekПотому убираю сортировку, тому что мой список уже отсортирован:
зачем сортировку убираешь?
for I in range(count): idxMin=I for J in range(I+1,count): if List[J]<List[idxMin]: idxMin=J temp=List[idxMin] List[idxMin]=List[I] List[I]=temp print(List)
Офлайн
0
sergeekМожете объяснить как это работает???if hf*2 == len(lst): return (lst[:hf][-1] + lst[hf:][0]) / 2.0 else: return lst[hf]
Офлайн
0
py.user.next
он имел в виду, что ты “неправильно” присоединяешь к списку, потому что надо вызывать метод у списка и передавать один аргумент
>>> lst =
>>> list.append(lst, 1)
>>> list.append(lst, 2)
>>> lst
>>>
while True: a= input("enter a number or Enter to finsh:") if a: try: number=int(a) except ValueError as err: print(err) continue total+=number count+=1 [b] list.append(List,number)[/b] Li=len(List) else: break
Офлайн
43
Agera
Отредактировано sergeek (Март 24, 2013 12:47:45)
Офлайн
253
:) Как всегда предложу короткий вариант в котором как и просит автор нет деления по модулю.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.median.html
import numpy as np print np.median([1,3,2,4,5])
Офлайн
0
doza_andСтандартные функции также нельзя использоватьКак всегда предложу короткий вариант в котором как и просит автор нет деления по модулю.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.median.html
import numpy as np
print np.median()
Отредактировано Agera (Март 24, 2013 14:47:07)
Офлайн
857
Ageraон не знает, что это работает, поэтому думает, что твой код не запускается
Почему неправильно я делаю так же.
Офлайн