简易的python计时器

导语

今天bug君用python写了一个小小的计时器,由于时间不是很够所以没有设计很多功能和内容,请谅解!以后有时间会不定期更新~


完整代码

此代码需要用到time的插件,如果没有安装请自行安装。

from time import sleep

print("欢迎使用计时器!")
print("作者:Debug618\n")
sleep(2)
tot = input("1.倒计时\n2.秒表\n请选择:")
if tot == "1":
    s = input("请输入倒计时时间:")
    timeout = int(input("是否计算超时时间?\n1.是\n2.否\n请回答:"))
    lens = len(s)
    x = ''
    hour = 0
    minu = 0
    sec = 0
    for i in range(lens):
        if s[i] == '时':
            hour = int(x)
            x = ''
        elif s[i] == '分':
            minu = int(x)
            x = ''
        elif s[i] == '秒':
            sec = int(x)
            x = ''
        elif 0 <= int(s[i]) <= 9:
            x += s[i]
    if sec >= 60:
        minu += int(sec / 60)
        sec = sec % 60
        print("您输入的秒数不符合标准,已自动为您纠正。")
    if minu >= 60:
        hour += int(minu / 60)
        minu = minu % 60
        print("您输入的分钟数不符合标准,已自动为您纠正。")
    if minu != 0 and hour != 0 and sec != 0:
        print("开始计时!\n")
    while hour != 0 or minu != 0 or sec != 0:
        sleep(1)
        sec -= 1
        if sec == -1:
            minu -= 1
            sec = 59
        if minu == -1:
            hour -= 1
            minu = 59
        if hour != 0:
            print(str(hour) + "小时", end='')
            if minu == 0 and sec == 0:
                print("\n", end='')
        if minu != 0:
            print(str(minu) + "分", end='')
            if sec == 0:
                print("\n", end='')
        if sec != 0:
            print(str(sec) + "秒")
    if timeout == 1:
        while hour <= 99999:
            sleep(1)
            sec += 1
            if sec == 60:
                minu += 1
                sec = 0
            if minu == 60:
                hour += 1
                minu = 0
            print("已超时", end = '')
            if hour != 0:
                print(str(hour) + "小时", end = '')
                if minu == 0 and sec == 0:
                    print("\n")
            if minu != 0:
                print(str(minu) + "分", end = '')
                if sec == 0:
                    print("\n")
            if sec != 0:
                print(str(sec) + "秒")
    print("0秒")
    print("计时结束!")
elif tot == "2":
    print("开始计时!\n")
    sec = 0
    minu = 0
    hour = 0
    while hour <= 99999:
        sleep(1)
        sec += 1
        if sec == 60:
            minu += 1
            sec = 0
        if minu == 60:
            hour += 1
            minu = 0
        if hour != 0:
            print(str(hour) + "小时", end = '')
            if minu == 0 and sec == 0:
                print("\n")
        if minu != 0:
            print(str(minu) + "分", end = '')
            if sec == 0:
                print("\n")
        if sec != 0:
            print(str(sec) + "秒")

    print("计时结束!")

print("\n感谢您的使用!")

结束语

好了,本期的内容就到这里了。如果对代码有改进建议可以评论或私信,我们下次再见,goodbye~

你可能感兴趣的:(python,#,python小游戏,python)