'module' 对象没有属性 'choice' - 尝试使用 random.choice [英] 'module' object has no attribute 'choice' - trying to use random.choice
本文介绍了'module' 对象没有属性 'choice' - 尝试使用 random.choice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能告诉我我做错了什么.运行 python 代码时,我不断收到此消息:
Could someone please tell me what I may be doing wrong. I keep getting this message when I run my python code:
import random
foo = ['a', 'b', 'c', 'd', 'e']
random_item = random.choice(foo)
print random_item
错误
AttributeError: 'module' 对象没有属性 'choice'
AttributeError: 'module' object has no attribute 'choice'
推荐答案
在黑暗中拍摄:您可能将脚本命名为 random.py
.不要将您的脚本命名为与模块相同的名称.
Shot in the dark: You probably named your script random.py
. Do not name your script the same name as the module.
我这样说是因为 random
模块确实有一个 choice
方法,所以导入可能是抓取了错误的(读取:不需要的)模块.
I say this because the random
module indeed has a choice
method, so the import is probably grabbing the wrong (read: undesired) module.
这篇关于'module' 对象没有属性 'choice' - 尝试使用 random.choice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文