rbhog
rbhog

Reputation: 5

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 would be printed, then the number 29 would replace it, and then 28 would replace it and so on.

def timer():
# I haven't made the counting down yet(sorry)
for i in range(30):
  print(i, end = '\r')

If anyone could help me that would be great.

Upvotes: 0

Views: 9347

Answers (3)

BXR Studios
BXR Studios

Reputation: 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

Upvotes: 0

USLTD
USLTD

Reputation: 309

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!')

Upvotes: 2

eyllanesc
eyllanesc

Reputation: 243897

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)

Upvotes: 2

Related Questions