0

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.

7
  • Can you explain what you mean by "next lowest number"? Commented Jan 7, 2017 at 0:51
  • What I meant was the printed item would be 30, then that would be replaced by 29, then 28, then so on. Commented Jan 7, 2017 at 16:57
  • You want to "overwrite" the previous number on the second tick. Is this correct? Commented Jan 7, 2017 at 17:02
  • Yes that is what I wanted to happen for each number. Commented Jan 7, 2017 at 17:09
  • Can you make that clearer in your question. This will help others that have the same problem. Commented Jan 7, 2017 at 17:24

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

You might want to add an explanation why \r does the trick. And careful when some output will be longer than other, as is already the case here.
@eyllanesc thanks for the suggestion. What I was thinking was that the 30 would get deleted and then 29 would show up on the same line. Is that possible?
so something I didn't make clear was that this project was in IDLE not in terminal.
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!')

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.