Passing Variable to Function
Brendan Abel
007brendan at gmail.com
Wed Oct 5 15:22:49 EDT 2016
Define your colors as actual number variables instead of a string
color = (255,0,0)
color2 = (0,0,255)
Then use argument expansion to pass them in as separate arguments to the
function
colorFunction(*color)
Brendan
On Wed, Oct 5, 2016 at 12:17 PM, John McKenzie <davros at bellaliant.net>
wrote:
>
> Hello, all.
>
> I have a function that takes three arguments, arguments to express an RGB
> colour. The function controls an LED light strip (a Blinkytape).
>
> Sometimes I might need to be change what colour is sent to the function,
> so I set a variable with the idea that I can change just the variable
> later if I need to instead of changing a bunch of different lines.
>
> So I have variables along the lines of this:
>
> colour ="255, 0, 0"
> colour2 ="100, 0, 0"
>
>
> My function, written by the Blinkytape people:
>
>
> def changeColor(r, g, b):
> serialPorts = glob.glob("/dev/ttyACM0*")
> port = serialPorts[0]
>
> if not port:
> sys.exit("Could not locate a BlinkyTape.")
>
> print "BlinkyTape found at: %s" % port
>
> bt = BlinkyTape.BlinkyTape(port)
> bt.displayColor(r, g, b)
> time.sleep(.1) # Give the serial driver some time to actually send
> the data
> bt.close()
>
>
> Later, I have conditional statements like:
>
>
> if latitude > maxNetural and latitude < NorthLine:
> changeColor(colour)
> elif latitude > NorthLine:
> changeColor(colour2)
>
>
>
> (There is a GPS device connected, there are variables defined based on
> latitude earlier in the script.)
>
> I get an error stating that changeColor takes three arguments and I am
> just giving it one (either "colour1" or "colour2").
>
>
> Is there a way to format the "colour" variable so that the changeColor
> function takes it as the three numbers it is meant to be defined as?
>
>
> Entire script:
> http://hastebin.com/qaqotusozo.py
>
>
> Thanks.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list