可以找到问题here
我正在尝试计算加权平均值,但是当我尝试使用for循环填充数组时它只是无所事事?
size = raw_input()
arr = raw_input()
w = raw_input()
deger = [1,2,2,2,2]
size = [int(i) for i in size.split()]
size = size[0]
arr = [int(i) for i in arr.split()]
w = [float(i) for i in w.split()]
def wm (x,y,s):
for i in range(0,s-1):
deger[i] = int(input(x[i]*y[i]))
return sum(deger)
print(wm(arr,w,size))
答案 0 :(得分:1)
在打印结果之前,您有一个return
。
答案 1 :(得分:1)
根据您的代码进行一些修改并进行适当的缩进:
size = raw_input()
arr = raw_input()
w = raw_input()
#deger = [1,2,2,2,2] # not necessary to initialize 'deger' here
size = [int(i) for i in size.split()]
size = size[0]
arr = [int(i) for i in arr.split()]
w = [float(i) for i in w.split()]
def wm (x,y,s):
deger = [] # initialize empty 'deger' here
for i in range(0,s): # 's-1' will not include the last item of x and y
deger.append(x[i]*y[i])
return sum(deger) / sum(y)
print('%.1f'%wm(arr,w,size)) # as 1 decimal place is required