任务描述
本关任务:本题中已给出一个时钟类的定义,请模拟数字时钟走字过程。
相关知识
为了完成本关任务,你需要掌握:1.类和对象。
编程要求
根据提示,在右侧编辑器补充代码,模拟数字时钟走字,只需输出60次走字。
测试说明
平台会对你编写的代码进行测试:
'''try:
fp=open('D:/python的txt/9.txt','r')
pass
except FileNotFoundError as q:
print(q)
pass
try:
a=float(input())
b=float(input())
c=a/b
except ValueError:
print('234')
else:
print(a/b)
import sys
try:
3/0
except:
tuple=sys.exc_info()
print(tuple)
from time import time, localtime, sleep
# 定义一个类描述数字时钟,建议尝试界面设计
class Clock(object):
"""数字时钟"""
def __init__(self, hour=0, minute=0, second=0):
"""
构造器
:param hour: 时
:param minute: 分
:param second: 秒
"""
self._hour = hour
self._minute = minute
self._second = second
@classmethod
def now(cls):
ctime = localtime(time())
return cls(ctime.tm_hour, ctime.tm_min, ctime.tm_sec)
def run(self):
"""走字"""
self._second += 1
if self._second == 60:
self._second = 0
self._minute += 1
if self._minute == 60:
self._minute = 0
self._hour += 1
if self._hour == 24:
self._hour = 0
"""时钟打印"""
def __str__(self):
return '%02d:%02d:%02d' % (self._hour, self._minute, self._second)
def main():
clock = Clock.now()
while True:
print(clock)
sleep(1)
clock.run()
if __name__ == '__main__':
main()
import json
def main(name):
# 请在此处添加代码 #
# *************begin************#
with open(name, 'r', encoding='utf-8') as f:
book_rows = []
content_json = []
for line in f.readlines():
line_list = line.strip('\n').split(' ')
book_rows.append(line_list)
fields = book_rows[0]
book_rows = book_rows[1:]
book_list = []
for row in book_rows:
temp_dict = dict()
for index, field in enumerate(fields):
temp_dict.update({field: row[index]})
book_list.append(temp_dict)
with open(name, 'w', encoding='utf-8') as fs:
json.dump(book_list, fs)
with open(name, 'r') as fs:
print(json.loads(fs.read()))
# **************end*************#
if __name__ == '__main__':
name = input()
main(name)'''
from time import sleep
class Clock(object):
"""数字时钟"""
def __init__(self, hour=0, minute=0, second=0):
"""初始化方法
:param hour: 时
:param minute: 分
:param second: 秒
"""
self._hour = hour
self._minute = minute
self._second = second
def run(self):
"""走字"""
self._second += 1
if self._second == 60:
self._second = 0
self._minute += 1
if self._minute == 60:
self._minute = 0
self._hour += 1
if self._hour == 24:
self._hour = 0
def show(self):
"""显示时间"""
return '%02d:%02d:%02d' % \
(self._hour, self._minute, self._second)
def main():
clock = Clock(00, 00, 00)
i=0
while i<60:
print(clock.show())
clock.run()
i=i+1
if __name__ == '__main__':
main()
; 预期输出:
23:59:58 23:59:59 00:00:00 00:00:01 00:00:02 00:00:03 00:00:04 00:00:05 00:00:06 00:00:07 00:00:08 00:00:09 00:00:10 00:00:11 00:00:12 00:00:13 00:00:14 00:00:15 00:00:16 00:00:17 00:00:18 00:00:19 00:00:20 00:00:21 00:00:22 00:00:23 00:00:24 00:00:25 00:00:26 00:00:27 00:00:28 00:00:29 00:00:30 00:00:31 00:00:32 00:00:33 00:00:34 00:00:35 00:00:36 00:00:37 00:00:38 00:00:39 00:00:40 00:00:41 00:00:42 00:00:43 00:00:44 00:00:45 00:00:46 00:00:47 00:00:48 00:00:49 00:00:50 00:00:51 00:00:52 00:00:53 00:00:54 00:00:55 00:00:56 00:00:57
|