pythontime⽤法(⼀)python time库的⽤法
time库导⼊
import time
time⽅法
时间戳(1970.1.1 0:0到现在的秒数)第3、8⾏
⽤法:time.time()
sleep⽅法
等待(秒数)第6⾏
⽤法:time.sleep(min)
实例滑冰作文
import time
_time_ = time.time()
print(_time_)
time.sleep(1)
_time1_ = time.time()
print(_time1_)
_time = _time1_ - _time_
print(_time)
input()
1⾏:
见
time库导⼊
3、8⾏:
设定变量_time(time1)_为当前时间戳
见
time⽅法
4、9⾏:
打印变量_time(time1)_
6⾏:
等待1秒
见
sleep⽅法
14⾏:
⽤输⼊禁⽌退出
这篇⽂章结束了,给个三连吧!
剩两个⽅法下节再介绍!
彩蛋
米芾书法⾮原创,作者优化
汉诺塔游戏
print("汉诺塔游戏")
#左中右塔⽤⼀个列表存储
left =list()
center =list()
right =list()
"""
初始化函数
"""
def init():
size =input("(请友善输⼊整数,未写判断!)请输⼊层数:")
#初始化塔列表,如5层左边塔放 1-3-5-7-9,中间和右边放5个-1 for i in range(1,int(size)+1):
left.append(i*2-1)
center.append(-1)
right.append(-1)
return int(size)
"""
打印样式函数
"""
def printStyling(i, size, ta):
if ta[i]!=-1:12月26日是什么星座
# 打印前空格
for kong in range(int(size -(ta[i]-1)/2)):
print(" ", end="")
# 打印塔元素
for le in range(ta[i]):
print("X", end="")
# 打印后空格
for kong in range(int(size -(ta[i]-1)/2)):
print(" ", end="")
# 左塔这⼀层为空格
el:
# 打印前⾯空格
for kong in range(size):
print(" ", end="")
# 打印中间的棒棒
print("|", end="")
# 打印后⾯的空格
for kong in range(size):
"""
控制台打印结果
"""
def show(size):
#修饰
print("-"*35)
#循环层数等于size
for i in range(size):
# 打印左边塔
printStyling(i, size, left)
# 打印中间塔
printStyling(i, size, center)
# 打印右边塔
printStyling(i, size, right)
#每⾏打印⼀个换⾏
学校英文print()
#修饰
print("-"*35)
"""
判断可不可以移动
takeOff减少,putOn增加,size层数,tSize和pSize剩余空间
四渡赤水路线图"""
def judge(takeOff, putOn, size, tSize, pSize, count):
1亩地是多少平方米
# 如果左塔的剩余空间等于size,就是空的,就没有元素可移动
if tSize == size:
print("操作⽆效!")
return0
# 如果中塔为空,可以移动
if pSize == size:
# 中间的最后⼀个元素赋上左塔的第⼀个元素的值
putOn[pSize -1]= takeOff[tSize]
# 左塔的第⼀个元素赋值-1
takeOff[tSize]=-1
# 左塔的剩余空间+1
tSize +=1
# 中塔的剩余空间-1
pSize -=1
产生问题的原因分析#步数+1
count +=1
#移动成功,返回剩余空间和步数
return tSize, pSize, count
# 如果中塔最上⽅元素⽐左塔最上⽅元素⼤,即可以移动
elif putOn[pSize]> takeOff[tSize]:
# 中塔当前最上⽅元素的再上⼀个元素(-1)赋上左塔最上⽅元素的值 putOn[pSize -1]= takeOff[tSize]
# 左塔最上⽅元素赋值-1
takeOff[tSize]=-1
# 左塔剩余空间+1
tSize +=1
# 中塔剩余空间-1
pSize -=1
#步数+1
count +=1
招聘信息范文# 移动成功,返回剩余空间和步数
return tSize, pSize, count
# 否则不可以移动
el:
return0
"""
主要运⾏函数
"""
def main():
#初始化游戏
size = init()
# 存放最初的盘剩余空间 lSize左塔 cSize中塔 rSize右塔 lSize =0
cSize = size
rSize = size
#存放操作步数
count =0
#打印游戏介绍
print("将左塔完整地移到右塔就是胜利!")
print("左-1 中-2 右-3 退出请输⼊:quit")
print('例如输⼊:"1-2"就是将左塔的最上元素放到中塔') print("%d层的最佳步数是%d"%(size,pow(2, size)-1))
#游戏进⾏
while True:
print("当前移动了%d步"%(count))
#显⽰当前塔的状态
show(size)
#判断右塔是否没有剩余空间,没有即胜利,并退出游戏
if rSize ==0:
if count ==pow(2, size)-1:
print("恭喜你使⽤最少步数完成汉诺塔!")
el:
sgjii =(pow(2, size)-1)
print("超出最少步数%d步"%(count-sgjii))
break
#获取玩家操作
lect =input("请操作:")
#左塔移中塔
if lect =="1-2":
result = judge(left, center, size, lSize, cSize, count) if result ==0:
continue
el:
lSize, cSize, count = result
#左塔移右塔,下⾯同样
elif lect =="1-3":
result = judge(left, right, size, lSize, rSize, count) if result ==0:
continue
el:
lSize, rSize, count = result
elif lect =="2-1":
result = judge(center, left, size, cSize, lSize, count) if result ==0:
continue
el:
cSize, lSize, count = result
elif lect =="2-3":
result = judge(center, right, size, cSize, rSize, count) if result ==0:
continue
el:
cSize, rSize, count = result
elif lect =="3-1":
if result ==0:
continue
el:
rSize, lSize, count = result
elif lect =="3-2":
result = judge(right, center, size, rSize, cSize, count) if result ==0:
continue
el:
rSize, cSize, count = result
#输⼊quit退出游戏
elif lect =="quit":
break
#如果输⼊的是其他不识别的⽂字,就拜拜
el:
print("操作有误!")
continue
main()
input("")