Python print() function not recalculating variables in a while loop

Question:

I am attempting to write a program that will calculate the difference in a given time and the actual time then display that delta in a while loop. I have been able to get most of this working, the only issue I have found so far is the time variables in the print statement do not update as the loop runs.

import datetime
import time
from os import system
from sys import platform

clear_screen = lambda: system("cls" if platform == "win32" else "clear")

# print("What is the time and date of your event?")
# year = int(input("Year: "))
# month = int(input("Month: "))
# day = int(input("Day: "))
# hour = int(input("Hour: "))
# minute = int(input("Minute: "))
i = 0
year = 2023
month = 1
day = 27
hour = 12
minute = 0
second = 00
today = datetime.datetime.now()
date_entry = datetime.datetime(year, month, day, hour, minute, second)

print(f"The current date & time: {today}")
print(f"The big day is: {date_entry}")
print()
print()
print(f"Tiff's Big day is going to be here soon:")
print()
event_count = date_entry - today
event_hour = event_count.total_seconds() / 3600
event_min = ((event_hour % 1) * (60 / 100)) * 100
event_sec = ((event_min % 1) * (60 / 100)) * 100


def countdown():
    print(f"{event_hour:.0f} Hours, {event_min:.0f} Minutes, {event_sec:.0f} seconds until big mode!!!!!")
    
while i < 50:
    i += 1
    countdown()
    time.sleep(2)
    # clear_screen()`

I have a feeling that the time variables in the print statement are not recalculating… I have tried restructuring the program by moving the variables into the countdown() function. That had the same result.

I am expecting the script to output hours, minutes and seconds until a defined time. This part works great. Then pause for 2 seconds (this works) then print the statement again after it recalculates the time delta. This is were it fails, prints the exact same time as in the first print statement.

You might also notice the clear_screen(). This kinda works, it will clear all of the output. I am looking to make it clear the last line printed in the loop (ie: 40 Hours, 12 Minutes, 56 seconds until big mode!!!!!) This is something I haven’t looked at much yet. If you have any suggestions…

Thanks in advance for any suggestions.

Output:
The current date & time: 2023-01-25 19:48:04.383425
The big day is: 2023-01-27 12:00:00

Tiff’s Big day is going to be here soon:

40 Hours, 12 Minutes, 56 seconds until big mode!!!!!
40 Hours, 12 Minutes, 56 seconds until big mode!!!!!
40 Hours, 12 Minutes, 56 seconds until big mode!!!!!
40 Hours, 12 Minutes, 56 seconds until big mode!!!!!
40 Hours, 12 Minutes, 56 seconds until big mode!!!!!

Asked By: 0331

||

Answers:

Be careful with calling time functions, the time is assigned to a variable only the first time, here is an example in the REPL:

>>> import time
>>> time.time()
1674700748.035392
>>> time.time()
1674700749.2911549
>>> time.time()
1674700750.440412
>>> time.time()
1674700751.571879
>>> x = time.time()
>>> x
1674700755.0605464
>>> x
1674700755.0605464
>>> x
1674700755.0605464
>>> x
1674700755.0605464
>>> for i in range(5): print(time.time())
... 
1674700912.1213877
1674700912.1214447
1674700912.1214585
1674700912.1214688
1674700912.1214786
>>> for i in range(5): print(x)
... 
1674700755.0605464
1674700755.0605464
1674700755.0605464
1674700755.0605464
1674700755.0605464

As you can see if I call time.time multiple times the time changes, but if I assign it to x, then x always has the same value.

Answered By: Caridorc

Below is the code I wrote to solve my problem:

import datetime
import time
from os import system
from sys import platform
import cursor

# print("What is the time and date of your event?")
# year = int(input("Year: "))
# month = int(input("Month: "))
# day = int(input("Day: "))
# hour = int(input("Hour: "))
# minute = int(input("Minute: "))

year = 2023
month = 1
day = 27
hour = 12
minute = 0
second = 00
date_entry = datetime.datetime(year, month, day, hour, minute, second)

print(f"The current date & time: {datetime.datetime.now()}")
print(f"The big day is: {date_entry}")
print()
print()
print(f"Tiff's Big day is going to be here soon:")
print()

while True:
    event_count = date_entry - datetime.datetime.now()
    event_hour = event_count.total_seconds() / 3600
    event_min = ((event_hour % 1) * (60 / 100)) * 100
    event_sec = ((event_min % 1) * (60 / 100)) * 100
    print(f"{event_hour:.0f} Hours, {event_min:.0f} Minutes, {event_sec:.0f} seconds until big time!!!", end = "r")
    cursor.hide()
    time.sleep(.5)
Answered By: 0331
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.