Share your repls and programming experiences

← Back to all posts
smartlightswitch
imightbenutella (3)

DISCLAIMER This is not an actual light switch. It is simply a beginner's Python project I made

lighty, lighty, are you on?

Basically, there is a smart light switch. It determines whether it is day or night, and if it is day it turns off. If it is night, it turns on. Simple? Yes. Boring? Yes. Complete? No. If you'd like to actually create a light switch - just the animation - go on. Me, I just started learning Python. I'll probably update this when I get time (read: learn how to draw/animate the light switch).

UPDATE!: The is_night no longer exists. The ifs all run on the light_on function.To turn the light on and off, use the is_day function.

Comments
hotnewtop
MikeJMS8910 (105)

Its lunch time right now and it says that it is night time

benji10 (25)

@MikeJMS8910
Looking at the code, this isn't supposed to get the time, it's literally just a hard-coded variable. Probably just to practice if statements.

benji10 (25)

Some suggestions, I guess...

if is_night:

would work too, I think, because is_night is true. It also looks slightly more readable to a human.

imightbenutella (3)

@benji10 yeah, but I was confused on how to put code inside that. I could do if is_night == True: {code} or {code} but I'm not sure if that'll work

benji10 (25)

@imightbenutella

Did you mean like:

if is_night:
     #code here like normal

like that?

imightbenutella (3)

@benji10 yeah, but I figured if I use light_on instead of is_night, it might be easier and much cleaner, so I tried that. It works as normal, and only the is_day needs to be changed, so I added a print telling whether is_day is True or False

benji10 (25)

@imightbenutella
Yeah, I think what I meant was something like this

is_day = True
light_on = not is_day

# light_on is already true, so we don't need to compare it to True

if light_on:
  print("The light is on!")
  print("And is it daytime?")
  print(is_day)

# Similar thing here, but you can use the not keyword to negate it

if not light_on:
  print("The light is off!")
  print("And is it daytime?")
  print(is_day)
imightbenutella (3)

@benji10 now that I think about it, the old code did look somewhat messy. Thanks for the tip!

benji10 (25)

@imightbenutella

You're welcome! Sometimes it's better to not over-comment, especially the more obvious parts. 😄