1

I'm trying to make a countdown timer like little script. I want it to look like this:

0 years left until your event.
0 months left until your event.
10 days left until your event.
234 hours left until your event.
14020 minutes left until your event.
841166 seconds left until your event.

As you can see it's not a "normal" countdown. I want it to be like this, total amount of seconds, total amount of minutes left etc. Now I'm running into trouble trying to make it actually count-down. Just the seconds are working fine, I'm using this piece of code for that:

def secondsLeft(eventDate):
    while secondsUntil:
        toPrint = "%s seconds left until your event." %(secondsUntil)
        print(toPrint, end='\r')
        time.sleep(1)
        secondsUntil -= 1

However, when trying to do the same for the minutes, replacing with time.sleep(60) it's not printing out the seconds anymore!

I think it has to do with the while loop. However, I don't know what to do about it... Any help in the right direction is appreciated!!

T Bruijnen
  • 13
  • 4
  • 2
    Isn't it obvious? Calculate the number of minutes, hours etc. **from** the number of seconds. – Błotosmętek Jun 07 '17 at 14:19
  • 2
    Alternatively, read a little about datetime objects, make your computations with a single datetime object and then just print it using the different granularities you want. – Josep Valls Jun 07 '17 at 14:20
  • Possible duplicate of [Looping at a constant rate with high precision for signal sampling](https://stackoverflow.com/questions/26774186/looping-at-a-constant-rate-with-high-precision-for-signal-sampling) – ivan_pozdeev Jun 07 '17 at 14:22
  • @Błotosmętek so you're saying throw everything into one line and calculation, into the def secondsLeft? At the moment I've put all the numbers seperate. – T Bruijnen Jun 07 '17 at 14:26
  • @JosepValls thanks for reminding me about that. I was using datetime in my script, however I thought this problem had to do more with the while loop and stuff in stead of the calculation part. I'll make sure to look into datetime – T Bruijnen Jun 07 '17 at 14:28
  • Thanks guys, you've pointed me in the right direction. Now I have something that works, however I need to clear out the entire command line every second to make it look pretty. Any way to use carriage return on a block of text in stead of one line of text? – T Bruijnen Jun 08 '17 at 06:33

1 Answers1

0

If you wait a minute the whole program will be stopped for a minute. It can't decrease the seconds. What you want to do is, keep waiting only a second per while interation and add this line mins, secs = divmod(secondsUntil, 60). You can print mins for the minutesUntil

EduardoMaia
  • 591
  • 1
  • 4
  • 17
  • Thank you for your answer. I can see how that works. However, I'm running into trouble printing the lines in the way I want to print them. The carriage return is giving me trouble, is there any alternative to this? – T Bruijnen Jun 07 '17 at 14:40
  • What do you mean when you say the CR is giving you trouble? – EduardoMaia Jul 05 '17 at 21:05