python3 时间模块 random模块之两个小练习
2020-02-13 23:55
295 查看
话不多说,一个是算时间的,还有一个是生成验证码的
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
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- python-时间模块,random、os、sys、shutil、json和pickle模块
- python时间模块和random模块
- python学习记录5--给自己:time,random模块的调用 循环判断练习
- Python之时间模块、random模块、json与pickle模块
- 使用random模块,生成动态随机密码 分类: python 小练习 python Module 2013-12-04 17:23 278人阅读 评论(0) 收藏
- Python学习笔记:3.3.4 时间相关模块
- python3--random模块
- python之time模块和random模块解析
- python3 时间模块常用方法
- Python之日期与时间处理模块(date和datetime)
- python标准模块 urllib 模块 和 第三方库requests 这两个模块都可以帮助我们后端 向别的服务器发请求
- Python 有两个内建的模块(getopt,optparse)用于处理命令行参数
- Python 中 datetime时间模块学习
- Python 常用内建模块(os, sys,random)
- Python random 模块
- Python中的random模块
- Python之time模块的时间戳、时间字符串格式化与转换(13位时间戳)
- python时间模块time,时间戳,结构化时间,字符串时间,相互转换,datetime
- python标准库之random模块
- Python 的格式化时间练习