当我调用字典d中的值时,出现这样的错误:'module' object is not callable
这就是我如何使用 d 中的值:
d = {'c1': (10,20), 'c2': (30,20), 'c3': (10,30), 'c4': (10,40)}
import math
import random
because there are two values for one key, I just need one random value of them, so I use random.choice, then I get the error:
'builtin_function_or_method' object has no attribute 'choice'
can somebody help me with this part?
time1=-random.choice(d['c1'])* math.log(1.0 - random())
time2=-random.choice(d['c2'])* math.log(1.0 - random())
time3=-random.choice(d['c3'])* math.log(1.0 - random())
time4=-random.choice(d['c4'])* math.log(1.0 - random())
更新: 我只需要将 random(( 更改为 random.random(( 来计算时间1
这是因为模块中有一个名为random
的random
函数。调用import random
时random
模块对代码可见,但对函数不可见random
。
试试这个(第 4 行有所不同(:
>>> d = {'c1': 10, 'c2': 20, 'c3': 40, 'c4': 8}
>>>
>>> import math
>>> from random import random
>>>
>>> time1=-d['c1']* math.log(1.0 - random())
>>> time2=-d['c2']* math.log(1.0 - random())
>>> time3=-d['c3']* math.log(1.0 - random())
>>> time4=-d['c4']* math.log(1.0 - random())
>>> time1
16.376979481808323
我还建议阅读更多关于模块的信息:https://docs.python.org/3/tutorial/modules.html
和执行模式:https://docs.python.org/3/reference/executionmodel.html
这完全是关于random
哪个模块。不能调用模块,而是调用模块中提供的函数。在我看来,您需要用random.randint(a, b)
替换random()
.无论如何,您可以从文档中找到所有受支持的函数。