DEV Community

Discussion on: What is the simplest code to explain a loop?

Collapse
 
0xyasser profile image
Yasser A
time = 00:00:00
SunIsSet = true
while (sunIsSet):
    print("it's night time!")
    time += 1
    if(time > 05:00:00 < 18:30:00):
       SunIsSet =false
       print("it's day time!")
Collapse
 
yaser profile image
Yaser Al-Najjar

love the idea, but it doesn't run actually

Traceback (most recent call last):
  File "python", line 1
    time = 00:00:00
             ^
SyntaxError: invalid syntax
Collapse
 
0xyasser profile image
Yasser A

Oh I was't expecting it to run, it meant to explain the idea of loops but here is one that works.

import datetime
time =datetime.datetime(100,1,1,0,0,0) # arbitrary day
sunIsSet = True
while (sunIsSet):
    if(time.hour > 5 and time.hour < 18):
       sunIsSet = False
       print("time is:",time.time().isoformat(), " it's day time!")
    else:
       print("time is:",time.time().isoformat()," it's night time!")
    time = time + datetime.timedelta(0,1)
Thread Thread
 
yaser profile image
Yaser Al-Najjar

oops... the imports killed the simplicity :(