gpt4 book ai didi

python - 随机模块没有属性 'choices'

转载 作者:行者123 更新时间:2023-12-02 01:59:50 25 4
gpt4 key购买 nike

我正在使用 Sublime Text3。我在 random 模块的 choices 属性方面遇到问题。我在任何路径或目录中都没有相同的名称。随机的其他属性工作得很好。

import random
import string

letters = string.ascii_lowercase
print(letters)
gen = random.choices(letters, k=16)
print(gen)

错误代码如下:

abcdefghijklmnopqrstuvwxyz
Traceback (most recent call last):
File "/home/anon/.config/sublime-text-3/Packages/User/test.py", line 6, in <module>
gen = random.choices(letters)
AttributeError: 'module' object has no attribute 'choices'

此问题的常见原因是什么?

最佳答案

Python 2 中没有 random.choices。您可以在 Python 2 中使用 random.sample

gen = random.sample(letters, k=16)

random.choices 包含在 Python 3 中

关于python - 随机模块没有属性 'choices',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com