Share your repls and programming experiences

← Back to all posts
Clock
LiamBerube (2)

I am new to python and the console in this version with the infinite loop is very chaotic I tried adding

import time as t
t.sleep(1)

To make it only do it every second because my smallest measurment is seconds but it always says something about pyflakes incorrect syntax. Can someone help in the comments?

Comments
hotnewtop
Brendan23 (137)

@LiamBerube you can use os.system('clear') or sys.stdout.write("write what you want here") if you have any questions, pls ask me.

LiamBerube (2)

@Brendan23 How would I get this to go on a tkinter window.
Also how could I clear the console after every log to only show one date time at once

Brendan23 (137)

@LiamBerube well, I'm gonna answer your second question first. to only show 1 date at once you could, simply, import os like this import os, and then put os.system("clear") inside the while loop where you actually print the date. example:

import os
import datetime
loop = 1
import time
while loop == 1:
	from datetime import timedelta 
	delta = timedelta(hours=+4)
	time.sleep(1)
        os.system("clear")
	now = datetime.datetime.now()-delta
	print ('The date and time is ' + str(now.year) + '/' +str(now.month) + '/' + str(now.day) + ' ' +str(now.hour) + ':' + str(now.minute) + ':' +str(now.second))

for that first question, I'll look in to it and get back to you.
also, I recommend from time import sleep instead of import time then you will have to change time.sleep(1) to sleep(1) so that the sleep function is less delayed.
here's the full code(with all the modifications.)

import datetime
import os
loop = 1
from time import sleep
while loop == 1:
  from datetime import timedelta 
  delta = timedelta(hours=+5)
  sleep(1)
  os.system("clear")
  now = datetime.datetime.now()-delta
  print ('The date and time is ' + str(now.year) + '/' + str(now.month) + '/' + str(now.day) + ' ' + str(now.hour) + ':' + str(now.minute) + ':' + str(now.second))