admin管理员组

文章数量:1487745

python生成随机四位数和AttributeError: module 'random' has no attribute 'sample'

python生成随机四位数和AttributeError: module 'random' has no attribute 'sample'

## AttributeError: module 'random' has no attribute 'sample'

##解决方法: ##原来是因为命名.py文件为random,和系统的有冲突导致的。 ##更改了脚本的名称,就可以解决了。 本地文件名称和系统的文件名称重名了。

代码语言:javascript代码运行次数:0运行复制
import string
import random


# 方法一
seeds = "0123456789"
random_str = []
for i in range(4):
     random_str.append(random.choice(seeds))
print("".join(random_str))

# 方法二
# seeds = string.digits
random_str = random.choices(seeds, k=4)
print("".join(random_str))

# 方法三
# seeds = string.digits
random_str = random.sample(seeds, k=4)
print("".join(random_str))

'''
打印输出如下:
9015
1104
5241
'''
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2023-04-03,如有侵权请联系 cloudcommunity@tencent 删除pythonattributeerrormodulerandomsample

本文标签: python生成随机四位数和AttributeError module x27randomx27 has no attribute x27samplex27