无法弄清楚我的代码中的小错误?

时间:2017-11-03 05:21:23

标签: python

我的代码问题3和4:

def avg_weight(diet, data):
acc = []
grams = data.items()
for (name , food , weights) in grams:
   if diet == food:
       acc.append([weights])
       total = sum(acc)
       avg = total / len(acc)
 return avg

以上我因某种原因出现acc.append([weights])行的语法错误

3:

def word_positions(phrase):
    str.split(phrase)
    for num in phrase:
        return (num + ":" + str(phrase[num])

由于某种原因,我收到了一个随机的EOF错误。

谢谢你们!

1 个答案:

答案 0 :(得分:0)

如果您期望的输出是:

0:t
1:e
2:s
3:t

然后:

def word_positions (phrase):
    str.split(phrase)
    for num in range(0,len(phrase)) :
        return str(num) + ":"+str(phrase[num])

对于另一个问题,请提供您期望的输入