猫史档案馆


【python】如何制作一个让你朋友惊呆的计时器

用户:嗨皮老六嗨皮老六查看:1 回复:4 评论:1 创建时间:2022-09-12T11:00:06


代码如下:

from time import sleep

set = ''
hou = 0
min = 0
sec = 0

while True:
	#初始化
	set = ''
	hou = 0
	min = 0
	sec = 0

	#设置
	while True:
		try:
			set = input('请设置秒>')
			sec = int(set)
		except(ValueError):
			print('\aError!')
			pass
		except(KeyboardInterrupt):
			print('\r')
		else:
			break

	while True:
		try:
			set = input('请设置分>')
			if set == '':
				min = 0
			else:
				min = int(set)
		except(ValueError):
			print('\aError!')
			pass
		except(KeyboardInterrupt):
			print('\r')
		else:
			break

	while True:
		try:  # 取消操作
			set = input('请设置时>')
			if set == '':
				hou = 0
			else:
				hou = int(set)
		except(ValueError):
			print('\aError!')
			pass
		except(KeyboardInterrupt):
			print('\r')
		else:
			break

	#转化
	if sec >= 60:
		min = min+sec//60
		sec = sec % 60
	if min >= 60:
		hou = hou+min//60
		min = min % 60

	#开始
	try:
		input('Start...')
		print('\n', end='')
		sec = sec-1
		while hou >= 0:
			while min >= 0:
				while sec >= 0:
					print('\r', hou, ' : ', min, ' : ', sec, '', end='')
					sleep(1)
					sec = sec-1
				min = min-1
				sec = 59
			hou = hou-1
			min = 59

		print('\r时间到!', ' '*5, '\n按 Ctrl+C 关闭\n')
		#利用KeyboardInterrupt实现Ctrl+C 关闭
		try:
			while True:
				print('\r\a', end='')
				sleep(0.6)
		except(KeyboardInterrupt):
			pass
	except(KeyboardInterrupt):
		print('\r已取消', ' '*(len(str(hou))+len(str(min))+len(str(sec))+6), '\n')

 不喜勿喷。


回复

上一页1 页 / 共 1下一页
嗨皮老六嗨皮老六

你们觉得怎么样

 

点赞0


评论


SCS_user_LoeheodVOQSCS_user_LoeheodVOQ

直接time.sleep不精准,时间长了会有误差

点赞0


评论


疯狂的ren疯狂的ren

import time as ti
import turtle as tu

def clock(a):
    for i in range(1,a,1):
        tu.write(a-i)
        ti.sleep(1)
        tu.clear()

clock(5)

点赞0


评论


疯狂的ren疯狂的ren

import time as t

def clock(a):
    for i in range(1,a,1):
        print(a-i)
        t.sleep(1)

clock(5)

点赞0


评论