Reputation: 546
def timer()
import time
x, y, z = 0, 0, 0
while True:
time.sleep(1)
print(str(z) + ":" + str(y) + ":" + str(x))
x += 1
if x == 3:
x = 0
y += 1
if y == 3:
x, y = 0, 0
z += 1
if z == 3:
x, y, z = 0, 0, 0
I tried returning the value but it would only as it would only return 0:0:0 once, and I cant figure out how to keep it constantly updating as a function.
Upvotes: 0
Views: 30
Reputation: 71479
Seems to work fine if you correct the syntax errors:
import time
def timer():
x, y, z = 0, 0, 0
while True:
time.sleep(1)
print(str(z) + ":" + str(y) + ":" + str(x))
x += 1
if x == 3:
x = 0
y += 1
if y == 3:
x, y = 0, 0
z += 1
if z == 3:
x, y, z = 0, 0, 0
if __name__ == '__main__':
timer()
0:0:0
0:0:1
0:0:2
0:1:0
0:1:1
0:1:2
0:2:0
0:2:1
If you were ever to decide to scale this timer up (e.g. having more spaces in the clock), storing the values in a list rather than in three hardcoded variables would give you more flexibility:
import time
def timer() -> None:
clock = [0] * 3
while True:
time.sleep(1)
print(":".join(str(d) for d in clock[::-1]))
i = 0
while True:
clock[i] += 1
if clock[i] < 3:
break
clock[i] = 0
i = (i + 1) % len(clock)
if __name__ == '__main__':
timer()
This gives the same output as-is, but you can make the "clock" arbitrarily larger by changing this line:
clock = [0] * 3
and you can make each digit count arbitrarily higher by changing this line:
if clock[i] < 3:
Upvotes: 1