Skip to content Skip to sidebar Skip to footer

Making A Timer In Python 3

So, I'm trying to make a countdown timer in Python. However, I am finding difficulty trying to replace the current printed number with the next lowest number. So, for example, 30 w

Solution 1:

You must use the range function with all its parameters.

range(start, stop[, step])

This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised)

If you want to replace a good option is to use the carriage return: "\ r" and change the print end "\n" to ""

import time

for x in range(30, 0, -1):
    print("\r %d" % x, end="")
    time.sleep(1)

Solution 2:

With your question title I think you want countdown timer, so here is my code to help you. Maybe this is your question's answer:

import time
hour = int(input('Enter any amount of hours you want -+==> '))
minute = int(input('Enter any amount of minutes you want -+==> '))
second = int(input('Enter any amount of seconds you want -+==> '))
time = hour*10800 + minute*3600 + second*60
print('{}:{}:{}'.format(hour,minute,second))
while time > 0:
   time = time - 1
   seconds = (time // 60) % 60
   minutes = (time // 3600)
   hours = (time // 10800)
   print('Time Left -+==> ',hours,':',minutes,':',seconds,)
if time == 0:
   print('Time Is Over!')

EDIT:

import os # For screen clear command
import time # For timer

hour = int(input('Enter any amount of hours you want -+==> '))
minute = int(input('Enter any amount of minutes you want -+==> '))
second = int(input('Enter any amount of seconds you want -+==> '))
time = hour*10800 + minute*3600 + second*60
print('{}:{}:{}'.format(hour,minute,second))
while time > 0:
   os.system("{}") # Replace "{}" as "CLS" for windows or "CLEAR" for other.
   time = time - 1
   seconds = (time // 60) % 60
   minutes = (time // 3600)
   hours = (time // 10800)
   print('Time Left -+==> ',hours,':',minutes,':',seconds,)
if time == 0:
   os.system("{}") # Replace "{}" as "CLS" for windows or "CLEAR" for other. 
   print('Time Is Over!')

Solution 3:

Well this is actually a good solution. It works as well and it is very simple.

First, import some modules

import os
import time

Now I recommend you create a function called whatever you like. I put timer. Then enter this code, you can rename the variable.

def timer(self):
    while self != 0:
        print(self)
        time.sleep(1)
        os.system('clear')
        self = self - 1

This is pretty simple as well and doesn't require a bunch of lines of code


Post a Comment for "Making A Timer In Python 3"