Posts: 83
Threads: 31
Joined: Aug 2020
def my_room_light_on():
my_room_l_on = b.set_light([1, 2, 9, 10], 'on', True)
my_room_l_off = b.set_light([1, 2, 9, 10], 'on', False)
if 'on' in text:
my_room_l_on
return True
elif 'off' in text:
my_room_l_off
return True
return False what would be the right way to write this i'm trying to return a command that would be in input with out having to make a whole bunch of functions trying to condense them. basically if on is in text I want it to return my_room_l_on if off is in text have it return the off variable.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Where is the text supposed to come from? If you're returning the object referred to by the my_room_l_on variable, then you don't have to return True also.
Posts: 83
Threads: 31
Joined: Aug 2020
the text is in a while loop after that so say its like this
while True:
text = input('cmd: ')
then if statements for example
if 'on' is in text:
I want to call that function
but I also want to be able to call that function for an if statement with off I only want it to return the one that's in the text I tried a couple of ways but it was returning them both so im assuming I did it wrong in the function
Posts: 8,169
Threads: 160
Joined: Sep 2016
Sep-15-2020, 04:43 AM
(This post was last modified: Sep-15-2020, 04:43 AM by buran.)
it's not clear what b in your code is/what library is used, but if I get what you want:
def light_switch(turn_on=True):
b.set_light([1, 2, 9, 10], 'on', turn_on) then if you want to turn lights on
light_switch(True) or just
light_switch() if you want to turn off
light_switch(False) now, as I said it's unclear what b is. so here (as well as in your code) it's a global variable, which is not ideal. there is a good chance that this could be implemented better
Posts: 1,583
Threads: 3
Joined: Mar 2020
Do you just want to return the result of the function, or do you want to return the function so it can be called later?
If you just want to return the result, it would be something like what you have, just return the right bits..
def my_room_light_on():
my_room_l_on = b.set_light([1, 2, 9, 10], 'on', True)
my_room_l_off = b.set_light([1, 2, 9, 10], 'on', False)
if 'on' in text:
return my_room_l_on
elif 'off' in text:
return my_room_l_off
return False
Posts: 83
Threads: 31
Joined: Aug 2020
Sep-15-2020, 06:59 AM
(This post was last modified: Sep-15-2020, 07:05 AM by Nickd12.)
@ buran b is how you call the light objects in Phue and @ bowlofred I tired that way but it seems to call both on and off any idea
@ buran that worked brilliantly say I wanted to adj the brightness with with 4 different objects
b.set_light([1, 2, 9, 10], 'bri', 254) the 254 is 100% on
so say I wanted to do 100% 75% 50% and 25% would I be able to do that with this as well
sorry if it seems like a stupid question learning as go
Posts: 8,169
Threads: 160
Joined: Sep 2016
Sep-15-2020, 07:09 AM
(This post was last modified: Sep-15-2020, 07:09 AM by buran.)
you can have as many positional and/or keyword parameters of the function and when you call it - pass respective arguments you wish and then use them in the body of the function.
That said, I would pass b as argument as well, thus making the function really reusable.
Posts: 83
Threads: 31
Joined: Aug 2020
Posts: 8,169
Threads: 160
Joined: Sep 2016
Sep-15-2020, 07:13 AM
(This post was last modified: Sep-15-2020, 07:14 AM by buran.)
Note that, what you are doing is actually replicating the set_light() method of b object (whatever it is). i.e. what you are doing is unnecessary. It looks like you wrap what you have in an additional layer of function. so you will just pass-trough all arguments. Instead of calling your function, just call the method with desired arguments :-)
Posts: 83
Threads: 31
Joined: Aug 2020
Yes the only reason I was trying to put them in my own function like that way is because some of the light functions like b.set_lights have different arguments with in them in addition they also have b.get_lights and b.get_group arguments but it made sense for me to wrap them again so I can make my own arguments now. I mean it’s probably not the cleanest way to do what I’m trying to do but like I said I’m learning as I go. Also b stands for bridge that runs the lights. Furthermore thank you it was a big help. :)
|