Search code examples
pythontimepython-os

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


I was trying to make a timer using python but when i run that code i get stuck with it .

This is my code.

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()

when i run it with giving the value 10 minutes it shows the error and this code run properly only when i use 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'

please solve this error


Solution

  • I have solved the problem of my code but i have still a problem. when i run the code by giving the value of 1 hour and 15 minute it start with wrong time else it work fine .

    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()