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
版权声明:本文标题:python生成随机四位数和AttributeError: module 'random' has no attribute 'sample' 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/shuma/1754979045a3182006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论