我在使用 Python 的导入随机函数时遇到问题。似乎import random
并且from random import random
正在导入不同的东西。我目前正在使用 Python 2.7.3
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> random()
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
random()
NameError: name 'random' is not defined
>>> random.randint(1,5)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
random.randint(1,5)
NameError: name 'random' is not defined
>>> import random
>>> random()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
random()
TypeError: 'module' object is not callable
>>> random.randint(1,5)
2
>>> from random import random
>>> random()
0.28242411635200193
>>> random.randint(1,5)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
random.randint(1,5)
AttributeError: 'builtin_function_or_method' object has no attribute 'randint'
>>>