Python 随机选择不是';行不通
我正在使用随机选择和字典,但它不起作用。此错误显示为 AttributeError:'function'对象在main.py的第90行没有属性'choice' (提前上课)Python 随机选择不是';行不通,python,dictionary,Python,Dictionary,我正在使用随机选择和字典,但它不起作用。此错误显示为 AttributeError:'function'对象在main.py的第90行没有属性'choice' (提前上课) 在代码的开头,您应该导入random import random 然后这样称呼它: random.randint(something) 或: 或者,如果需要,您可以只导入randint和choice: from random import randint,choice 然后你可以写: randint(something
在代码的开头,您应该导入random
import random
然后这样称呼它:
random.randint(something)
或:
或者,如果需要,您可以只导入randint和choice:
from random import randint,choice
然后你可以写:
randint(something)
choice(something)
如果您想了解有关随机模块的更多信息,请访问以下文档链接:您似乎没有在粘贴的代码中使用
random.choice
。但是,第一个猜测是您在某处调用了函数random
,并覆盖了对模块的引用。这个问题无法重现,因为您没有向我们展示如何导入random
模块或如何调用random.choice
。您应该能够编写一个只有几行的小示例程序来演示这个问题。
from random import randint,choice
randint(something)
choice(something)