2

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

11
  • 4
    Why would you expect anything to be recalculated? The only variable whose value changes inside your loop is i. Commented Jan 26, 2023 at 2:28
  • My thought was since I am calling the print statement again in the loop the variables would change since the time had changed. I had a suspicion of what you are saying and tried some Googleing but didn't find a good solution. If anyone could give me the correct topic to read up on, I would appreciate it. Not looking for anyone to write the answer out just need a jumping off point. Commented Jan 26, 2023 at 2:36
  • 3
    @TomKarzes This is a common beginner misunderstanding. They think that variables are replaced with the expression that defined them. Commented Jan 26, 2023 at 2:41
  • 2
    Tom, I understand what you are saying and figured this might be the issue. In my troubleshooting process I moved all of the variables into the function to no avail. If you know of a topic I could read up on that would help that would be great. Commented Jan 26, 2023 at 2:42
  • 1
    If you know of a topic I could read up on There's nothing to read, really. In Python, variables keep the original value they are assigned, until you explicitly assign a new value. They do not dynamically change based on some formula. Commented Jan 26, 2023 at 2:46

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Caridorc, Thanks, That makes sense to me. I will work on implementing that.
@0331 keep in mind that this works for all functions and not only time, for example the same is true for the random.random function and of functions that depend on variables that changed in the meantime
I was able to get my script working using a while loop with my variables in it. Thanks for your help cleared up some confusion in my pea brain. It is kinda crazy, I can get certain things without issue but sometimes a simple problem will cost me hours to figure out. Would you consider this a valid question for Stackoverflow or are these beginner questions better suited for another site? I try not to ask for help until i get frustrated and have exhausted google.
@0331 I am glad to have helped, if you have used google and tried to solve the problem on your own and the question is clear with enough code and a clear error and expected output, then you are welcome to ask any kind of question, including simple ones. They enrich the site because they may also help future visitors.
1

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)

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.