I can't understand why the auto_clock() function can work properly but the clock_time_add() function. The first function would print out a column of lists from 1, 2, 3, ...which is expected while the second would just print out a column of zeros.
This is output of the first function: enter image description here
And this is the output of the second one: enter image description here
And here is the code:
import time
def auto_clock():
second, minute, hour, day = -1, 0, 0, 1
while True:
second += 1
time.sleep(1)
if second == 60:
second = 0
minute += 1
if minute == 60:
minute = 0
hour += 1
if hour == 24:
hour = 0
day += 1
time_clock = [day, hour, minute, second]
return time_clock
def clock_time_add():
while True:
time_clock = auto_clock()
print(time_clock)
clock_time_add()
2 Answers 2
In the second function, you are not passing updated values after one second. That is why, the first function is being called with the same second, minute, hour, and day value.
I have updated the code to pass the updated value to the first function.
import time
def auto_clock(second, minute, hour, day):
second += 1
if second == 60:
second = 0
minute += 1
if minute == 60:
minute = 0
hour += 1
if hour == 24:
hour = 0
day += 1
return second, minute, hour, day
def clock_time_add():
second, minute, hour, day = 0, 0, 0, 1
while True:
second, minute, hour, day = auto_clock(second, minute, hour, day)
print([day, hour, minute, second])
time.sleep(1)
clock_time_add()
Output for first 100 records:
[1, 0, 0, 1]
[1, 0, 0, 2]
[1, 0, 0, 3]
[1, 0, 0, 4]
[1, 0, 0, 5]
[1, 0, 0, 6]
[1, 0, 0, 7]
[1, 0, 0, 8]
[1, 0, 0, 9]
[1, 0, 0, 10]
[1, 0, 0, 11]
[1, 0, 0, 12]
[1, 0, 0, 13]
[1, 0, 0, 14]
[1, 0, 0, 15]
[1, 0, 0, 16]
[1, 0, 0, 17]
[1, 0, 0, 18]
[1, 0, 0, 19]
[1, 0, 0, 20]
[1, 0, 0, 21]
[1, 0, 0, 22]
[1, 0, 0, 23]
[1, 0, 0, 24]
[1, 0, 0, 25]
[1, 0, 0, 26]
[1, 0, 0, 27]
[1, 0, 0, 28]
[1, 0, 0, 29]
[1, 0, 0, 30]
[1, 0, 0, 31]
[1, 0, 0, 32]
[1, 0, 0, 33]
[1, 0, 0, 34]
[1, 0, 0, 35]
[1, 0, 0, 36]
[1, 0, 0, 37]
[1, 0, 0, 38]
[1, 0, 0, 39]
[1, 0, 0, 40]
[1, 0, 0, 41]
[1, 0, 0, 42]
[1, 0, 0, 43]
[1, 0, 0, 44]
[1, 0, 0, 45]
[1, 0, 0, 46]
[1, 0, 0, 47]
[1, 0, 0, 48]
[1, 0, 0, 49]
[1, 0, 0, 50]
[1, 0, 0, 51]
[1, 0, 0, 52]
[1, 0, 0, 53]
[1, 0, 0, 54]
[1, 0, 0, 55]
[1, 0, 0, 56]
[1, 0, 0, 57]
[1, 0, 0, 58]
[1, 0, 0, 59]
[1, 0, 1, 0]
[1, 0, 1, 1]
[1, 0, 1, 2]
[1, 0, 1, 3]
[1, 0, 1, 4]
[1, 0, 1, 5]
[1, 0, 1, 6]
[1, 0, 1, 7]
[1, 0, 1, 8]
[1, 0, 1, 9]
[1, 0, 1, 10]
[1, 0, 1, 11]
[1, 0, 1, 12]
[1, 0, 1, 13]
[1, 0, 1, 14]
[1, 0, 1, 15]
[1, 0, 1, 16]
[1, 0, 1, 17]
[1, 0, 1, 18]
[1, 0, 1, 19]
[1, 0, 1, 20]
[1, 0, 1, 21]
[1, 0, 1, 22]
[1, 0, 1, 23]
[1, 0, 1, 24]
[1, 0, 1, 25]
[1, 0, 1, 26]
[1, 0, 1, 27]
[1, 0, 1, 28]
[1, 0, 1, 29]
[1, 0, 1, 30]
[1, 0, 1, 31]
[1, 0, 1, 32]
[1, 0, 1, 33]
[1, 0, 1, 34]
[1, 0, 1, 35]
[1, 0, 1, 36]
[1, 0, 1, 37]
[1, 0, 1, 38]
[1, 0, 1, 39]
[1, 0, 1, 40]
Comments
Consider that the while loop in your first function has no purpose, as the return time_clock means it will exit on its first iteration. Indentation is critical in Python.
In time_clock_add you're calling auto_clock over and over with a single iteration, with the same starting point, so the same thing will be printed each time.
sleep(1)will not sleep exactly one second and in combination with time it takes to run the code there might be a drift in your clock rather quickly if you run it in a loop.