[python学习]案例01:随机验证码与账号密码修改

第一次发博客,记录自学python时写的案例(有其他语言基础),如有建议感谢施教。

随机验证码

python 复制代码
import random
import string
print(string.ascii_lowercase)# 小写字母
print(string.ascii_uppercase)# 大写字母
print(string.digits)# 数字
all_char = string.ascii_lowercase + string.ascii_lowercase + string.digits
# choice("")从括号内选一个字符返回
random_char = random.choice(all_char)
print("random_char:", random_char)
s = ""
for i in range(5):
    s = s+random.choice(all_char)
print(s)

运行图片

账号密码修改

python 复制代码
import random
import string
# 原始用户名和密码
userName = "admin"
password = "passwd"
# 大小写字母和数字
all_char = string.digits+string.ascii_lowercase+string.ascii_uppercase
u_name = input("请输入用户名")
p_wd = input("请输入密码")
# 生成验证码
ver_code = ""
for i in range(5):
    ver_code += random.choice(all_char)
print(f"验证码:{ver_code}")
in_ver_code = input("请输入验证码")
# 判断用户名密码和验证码
if userName == u_name and password == p_wd and ver_code == in_ver_code:
    print("1.登录")
    print("2.修改密码")
    num = input("请输入序号")
    # 登录
    if num == "1":
        print("登录成功")
    # 修改密码
    if num == "2":
        password = input("请输入新密码")
        print("修改成功")
else:
    print("输入错误!")

运行图片

总结

该案例重点是string的三个内置模块string.ascii_lowercase(小写字母),string.ascii_uppercase(大写字母), string.digits(数字)。

相关推荐
AI云海1 小时前
python 列表、元组、集合和字典
开发语言·python
二十雨辰1 小时前
[爬虫]-Urllib
爬虫·python
玉鸯3 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260854 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao4 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
qetfw4 小时前
MXU:Tauri 2 + React 的 MaaFramework 跨平台 GUI 源码
前端·python·react.js·前端框架·开源项目·效率工具
用户8356290780515 小时前
Python 实现 Excel 页面布局与打印设置自动化
后端·python
一次旅行5 小时前
Python+大模型端到端自动化日报系统
开发语言·python·自动化
天天爱吃肉82185 小时前
【电源拆解:跟着问答逐一认识开关适配器PCB元器件】
大数据·人工智能·python·功能测试·嵌入式硬件·汽车