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

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

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

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
'''

python生成随机四位数和AttributeError: module 'random' has no attribute 'sample'的更多相关文章

  1. python 脚本运行时报错: AttributeError: 'module' object has no attribute ***

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  2. Python 3.6安装yaml时报"AttributeError: module 'pip' has no attribute 'main'"和“Non-zero exit code”错误

    1.Python 3.6安装yaml时一开始报AttributeError: module 'pip' has no attribute错误,根据网上提供的解决方法修改Pycharm安装目录D:\Pr ...

  3. python中提取位图信息(AttributeError: module 'struct' has no attribute 'unstack')

    前言 今天这篇博文有点意思,它是从一个例子出发,从而体现出在编程中的种种细节和一些知识点的运用.和从前一样,我是人,离成神还有几十万里,所以无可避免的出现不严谨的地方甚至错误,请酌情阅读. 0x00 ...

  4. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  5. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  6. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

  7. python文件名不要跟模块名相同,报错AttributeError: 'module' object has no attribute 'Differ'

    python中的文件都会生成pyc文件,包括模块也是这样,所以调用模块的时候,实际上会调用模块.pyc文件:在这个前提下,如果将文件名命名成跟模块名一样,在同一目录下就会生成一个跟模块名一样的pyc文 ...

  8. JavaScript 编写随机四位数验证码(大小写字母和数字)

    1.JavaScript编写随机四位数验证码,用到的知识点为: a.Math对象的随机数:Math.random() b.Math对象的取整    :Math.floor() c.处理所需要的下标个数 ...

  9. python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法?

    python中引入包的时候报错:import unittestimport smtplibimport timeimport osimport sysimp.reload(sys)sys.setdef ...

  10. python生成随机日期字符串

    python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 # ...

随机推荐

  1. [Blockchain] Cosmos Starport 地址前缀的变更方式

    # 在新的区块链上修改 starport app github.com/hello/planet --address-prefix your_new_prefix # 在已存在的区块链上修改 `app ...

  2. [PHP] Laravel auth:airlock 更名 auth:sanctum

    本以为有了一种改善型的服务出来了,没想到不是. 关于 Laravel 现有的三大验证方式看这里:[PHP] 浅谈 Laravel 三大验证方式的区别, auth:api, passport, auth ...

  3. WPF 布局 在有限空间内让两个元素尽可能撑开的例子

    我在尝试写一个显示本机 WIFI 热点的账号和密码的控件,要求此控件在有限的空间内显示.但是尽可能显示出热点的账号和密码.而热点的账号和密码是用户配置的,也许长度很长.我的需求是在假如账号的长度较短的 ...

  4. html+jq实现全国省的单选,弹框输入input

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  5. Oracle和达梦:根据外键名字查询表名

    根据外键名字查询表名 select * from user_cons_columns cl where cl.constraint_name = '外键名';

  6. 从[SDOI2011]消防 到[NOIP2007]树网的核

    有关消防一题中最优解一定在直径上的证明 P2491 [SDOI2011] 消防 P1099 [NOIP2007 提高组] 树网的核 题目描述 在一颗 \(n\) 个节点的无根树中,找到一条不超过 \( ...

  7. Spring6 当中的 Bean 循环依赖的详细处理方案+源码解析

    1. Spring6 当中的 Bean 循环依赖的详细处理方案+源码解析 @ 目录 1. Spring6 当中的 Bean 循环依赖的详细处理方案+源码解析 每博一文案 1.1 Bean的循环依赖 1 ...

  8. [Oracle故障处理]ORA-30012: undo tablespace 'UNDOTBS1' does not exist

    场景:用RMAN在异机上恢复数据. 错误信息如下: ORA-01092: ORACLE instance terminated. Disconnection forced ORA-30012: und ...

  9. three.js教程7-PBR材质与环境贴图CubeTextureLoader

    1.PBR材质 PBR是基于物理的渲染(physically-based rendering).模拟物体表面的反射算法. Three.js提供了两个PBR材质相关的类MeshStandardMater ...

  10. 远程桌面使用Pr剪视频

    要远程访问高性能计算机并使用 Pr(Adobe Premiere Pro)进行视频编辑,您可以考虑使用流畅且响应迅速的远程桌面软件.您可以考虑以下选项. Splashtop Business Acce ...