没有bug队——加贝——Python 练习实例 9,10

 9.题目:

暂停一秒输出。

程序分析:使用 time 模块的 sleep() 函数。

注:dict.items表示取出字典中的值

代码:

#9
import time
 
myD = {1: 'a', 2: 'b'}
for key, value in dict.items(myD):
    print (key, value)
    time.sleep(1) # 暂停 1 秒

输出:

10. 题目:

暂停一秒输出,并格式化当前时间。

注:localtime表示取出当地现在时间

#10
import time
 
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
 
# 暂停一秒
time.sleep(1)
 
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

输出:

猜你喜欢

转载自blog.csdn.net/qq_25990967/article/details/121525890