使用 python 制作计时器时出错(以 10 为底的 int() 的无效文字)

Error while making timer using python ( invalid literal for int() with base 10 )

我试图使用 python 制作一个计时器,但是当我 运行 该代码时,我遇到了问题。

这是我的代码。

import os
import time

while True:
    timer = input("enter the time when to stop the timer =  ")
    if "second" in timer or "seconds"in timer:
        t= timer.replace("second","").replace("seconds","")
    elif "minutes" in timer or "minute" in timer:
        t=timer.replace("minutes",'*60').replace("minute",'*60')
    elif "hour" in timer or "hours" in timer:
        t= timer.replace("hour","*60*60").replace("hours","60*60")
    elif "hour" in timer or "hours" in timer and "minutes" in timer or "minute" in timer:
        t=timer.replace("hour","*60*60").replace("hours","60*60").replace("and","+").replace("minutes","*60").replace("minute","*60")
    else:
        print("write valid number")

    when_to_stop =abs(int(t))
    while when_to_stop >0:
        os.system('cls')
        m,s = divmod(when_to_stop,60)
        h,m = divmod(m,60)
        print("\n\n\n\n\n\n\n\n\n")
        print("\t\t\t\t|"+str(h).zfill(2)+":" + str(m).zfill(2)+":"+str(s).zfill(2)+"|")
        time.sleep(1)
        when_to_stop -=1
        print()
    print("\t\t\t\tTIME OUT")
    exit()

当我 运行 它给出值 10 minutes 时,它显示错误并且此代码 运行 只有当我使用 10 second.

时才正确
enter the time when to stop the timer =  10 minutes
Traceback (most recent call last):
  File "E:\mycodes\python codes\testcode.py", line 35, in <module>
    when_to_stop =abs(int(t))
ValueError: invalid literal for int() with base 10: '10 *60'

请解决这个错误

您正在尝试将“10 *60”字符串转换为整数。如果将 print(t) 放在每个测试条件中,您可以看到当代码到达时:

when_to_stop =abs(int(t))

t等于一个字符串,无法转换为整数。

不使用替换,您可以简单地保留条件结构,然后将字符串剥离为整数并根据需要相乘。

import re
timer = input("enter the time when to stop the timer =  ")
    if "second" in timer or "seconds"in timer:
        t = re.findall(r'\d+', timer)  # this returns a list containing the integer like so ['10'] for '10 seconds'

这是第一个循环的完整部分:

    while True:
        timer = input("enter the time when to stop the timer =  ")
        if "second" in timer or "seconds"in timer:
            t = re.findall(r'\d+', timer)
            totalTime = t[0]
        elif "minutes" in timer or "minute" in timer:
            t = re.findall(r'\d+', timer)
            totalTime = t[0] * 60
        elif "hour" in timer or "hours" in timer:
            t = re.findall(r'\d+', timer)
            totalTime = t[0] * 60 * 60
        elif "hour" in timer or "hours" in timer and "minutes" in timer or "minute" in timer:
            t = re.findall(r'\d+', timer)
            totalTime = (t[0] * 60 * 60) + (t[1] * 60)

        else:
            print("write valid number")

totalTime 因此是以秒为单位的总时间。我 运行 这个可以工作,但以小时为单位显示时间不起作用,但分钟和秒可以。这是一个关于如何处理 totalTime 秒数以显示计数器倒计时的单独问题。

根据测试,我 运行 这段代码应该可以正常运行 - 如果它不适合您,请告诉我。

import os
import time
import re


while True:
    timer = input("enter the time when to stop the timer =  ")
    if "second" in timer or "seconds"in timer:
        filtering = re.findall(r'\d+', timer)
        timer = int(''.join(filtering))
    elif ('minutes' not in timer and "minute" not in timer) and ("hour" in timer or "hours" in timer):
        filtering = re.findall(r'\d+', timer)
        timer = int(''.join(filtering))*3600
    elif ("hour" in timer or "hours" in timer) and ("minutes" in timer or "minute" in timer):
        print('hello')
        filtering = re.findall(r'\d+', timer)
        a=(int(''.join(filtering[0]))*3600)
        b=(int(''.join(filtering[1]))*60)
        timer =a+b
    elif "minutes" in timer or "minute" in timer:
        filtering = re.findall(r'\d+', timer)
        timer = int(''.join(filtering))*60
    else:
        print("Please! write valid number")

    when_to_stop =abs(int(timer))
    while when_to_stop >0:
        os.system('cls')
        m,s = divmod(when_to_stop,60)
        h,m = divmod(m,60)
        print("\t\t\t\t|"+str(h).zfill(2)+":" + str(m).zfill(2)+":"+str(s).zfill(2)+"|")
        time.sleep(1)
        when_to_stop -=1
        print()
    print("\t\t\t\tTIME OUT")
    exit()

我已经解决了我的代码问题,但我仍然有问题。 当我 运行 代码给出 1 hour and 15 minute 的值时,它以错误的时间开始,否则它工作正常。

import os
import time
import re
while True:
    timer = input("enter the time when to stop the timer =  ")
    if "second" in timer or "seconds"in timer:
        filtering = re.findall(r'\d+', timer)
        timer = int(''.join(filtering))
    elif "minutes" in timer or "minute" in timer:
        filtering = re.findall(r'\d+', timer)
        timer = int(''.join(filtering))*60
    elif "hour" in timer or "hours" in timer:
        filtering = re.findall(r'\d+', timer)
        timer = int(''.join(filtering))*3600
    elif "hour" in timer or "hours" in timer and "minutes" in timer or "minute" in timer:
        filtering = re.findall(r'\d+', timer)
        a=(int(''.join(filtering[0]))*3600)
        b=(int(''.join(filtering[1]))*60)
        timer =a+b
    else:
        print("Please! write valid number")

    when_to_stop =abs(int(timer))
    while when_to_stop >0:
        os.system('cls')
        m,s = divmod(when_to_stop,60)
        h,m = divmod(m,60)
        print("\n\n\n\n\n\n\n\n\n")
        print("\t\t\t\t|"+str(h).zfill(2)+":" + str(m).zfill(2)+":"+str(s).zfill(2)+"|")
        time.sleep(1)
        when_to_stop -=1
        print()
    print("\t\t\t\tTIME OUT")
    exit()