个人整理Python代码实例

 

1。四位数字字母验证码的生成实例

2cded7531585a15bc91a33f85e4c8089.gif

 1 import random    
 2 if __name__ =="__main__":    #四位数字字母验证码的生成
 3     checkcode="" #保存验证码的变量
 4     for i in range(4):
 5         index=random.randrange(0,4)  #生成一个0~3中的数
 6         if index!=i and index +1 !=i:
 7             checkcode +=chr(random.randint(97,122))  # 生成a~z中的一个小写字母
 8         elif index +1==i:
 9             checkcode +=chr(random.randint(65,90) ) # 生成A~Z中的一个大写字母
10         else:
11             checkcode +=str(random.randint(1,9))  # 数字1-9
12     print(checkcode)

468be349dce077d335c3b5b6fe96b238.gif

输出为:m47A、8wQ9、vugS

-------------------------------

2。格式化时间函数

1 def formatTime(longtime):
2 '''格式化时间的函数'''
3 import time
4 return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(longtime))

--------------------------------

3。记录显示登录日志实例

9b0d83d51c24c42a243163ddb4633adf.gif

import time

def show_info():
    print('''输入提示数字,执行相应操作
0:退出
1:查看登录日志
    ''')

def write_loginfo(username):
    """
    将用户名和登录时间写入日志
    :param username: 用户名
    """
    with open('log.txt','a') as f:
        string = "用户名:{} 登录时间:{}\n".format(username ,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
        f.write(string)

def read_loginfo():
    """
    读取日志
    """
    with open('log.txt','r') as f:
        while True
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

LHStudio感谢您的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值