-1

我收到以下代码的属性错误:

coininfo = [ {} for k in range(0,numberOftrials)]

coininfo[i].append([x,outcome(x)])

以下是我得到的确切错误屏幕:

Traceback (most recent call last):
   File "pr1.py", line 22, in <module>
runsimulation(numberOftrials,numberOfcoins)
  File "pr1.py", line 19, in runsimulation
coininfo[i].append([x,outcome(x)])
AttributeError: 'dict' object has no attribute 'append'

任何帮助表示赞赏!

4

1 回答 1

0

当你跑

coininfo = [ {} for k in range(0,numberOftrials)]

你最终得到一个字典数组,而不是数组数组。因此coininfo[i]是一本字典,你不能附加到它上面。

我的猜测是您想将第一行更改为

coininfo = [ [] for k in range(0,numberOftrials)]

所以你将有一个数组数组。或者,如果你的意思是你的输出是一个字典数组,你可能意味着有

coininfo[i][x] = outcome(x)

代替

coininfo[i].append([x,outcome(x)])
于 2013-10-13T07:48:21.853 回答