python3 时间模块 random模块之两个小练习
2019-05-08 07:30:02来源:博客园 阅读 ()
话不多说,一个是算时间的,还有一个是生成验证码的
1 #!usr/bin/env/ python 2 # -*- coding:utf-8 -*- 3 # Author: XiaoFeng 4 import time 5 start_time = "2016-9-1 7:00:00" 6 handel = time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S")) 7 now = time.time() 8 now_strtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now)) 9 print(now_strtime) 10 dis_time = now - handel 11 struc_time = time.localtime(dis_time) 12 print("我已经来这儿\033[41;1m%d年,%d月,%d日,%d小时,%d分钟,%d秒了!\033[0m,好快呀" % 13 (struc_time.tm_year-1970, struc_time.tm_mon-1, struc_time.tm_mday-1, 14 struc_time.tm_hour, struc_time.tm_min, struc_time.tm_sec))
验证码:
#!usr/bin/env/ python # -*- coding:utf-8 -*- # Author: XiaoFeng import random def verif_code(): code = [] i = 4 while 1: times_1 = random.sample(["num", "alpha"], 1) print(times_1) if times_1[0] == "num": num = str(random.randint(0, 9)) code.append(num) else: alpha = chr(random.randint(65, 90)) if random.randint(0, 1) == 0: alpha = alpha.lower() code.append(alpha) i -= 1 if i == 0: return code verification = verif_code() print(verification) verification = "".join(verification) print(verification)
#!usr/bin/env/ python
# -*- coding:utf-8 -*-
# Author: XiaoFeng
import random
def verif_code():
code = []
i = 4
while 1:
times_1 = random.sample(["num", "alpha"], 1)
print(times_1)
if times_1[0] == "num":
num = str(random.randint(0, 9))
code.append(num)
else:
alpha = chr(random.randint(65, 90))
if random.randint(0, 1) == 0:
alpha = alpha.lower()
code.append(alpha)
i -= 1
if i == 0:
return code
verification = verif_code()
print(verification)
verification = "".join(verification)
print(verification)
原文链接:https://www.cnblogs.com/xf1262048067/p/10810515.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- python3 enum模块的应用 2019-08-13
- pycharm 新建py文件写时有作者和时间 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash