![图片[1]-如何使用python random模块中的randint()函数?-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/1608175448446114.png)
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
1、random.randint()函数原型
random.randint(a,b)
用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
2、使用语法
importrandom r=random.randint(a,b)
随机产生a-b之间的整数,包括a和b。
3、使用
示例一:生成随机整数1~100
#-*-coding:UTF-8-*- importrandom #随机整数 print(random.randint(1,100)
示例二:随机生成整数验证码
importrandom
#从一个字符串中随机生成若干个字符
defgen_code(n):
s='er0dfsdfxcvbn7f989fd'
code=''
foriinrange(n):
r=random.randint(0,len(s)-1)
code+=s[r]
returncode
deflogin():
username=input("输入用户名:")
passwd=input("输入密码:")
code=gen_code(5)
print("验证码是:",code)
code1=input("输入验证码:")
ifcode.lower()==code1.lower():
ifusername=='knn'andpasswd=='abc':
print("Loginsuccess!")
else:
print("usernameorpassworderror!")
else:
print("checkcodeerror!")
#调用函数
login()原文来自:https://www.py.cn © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END



















































暂无评论内容