Passing Variable to Function

alister alister.ware at ntlworld.com
Wed Oct 5 15:23:08 EDT 2016


On Wed, 05 Oct 2016 19:17:33 +0000, John McKenzie 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.

use a list & pythons parameter expansion options (I am sure the experts 
here will be able to provide the correct name)

colour1=[100,0,255]

def set_colour(r,g,b):
	<code goes here>


set_colour(*colour1)



-- 
Come live with me and be my love,
And we will some new pleasures prove
Of golden sands and crystal brooks
With silken lines, and silver hooks.
There's nothing that I wouldn't do
If you would be my POSSLQ.

You live with me, and I with you,
And you will be my POSSLQ.
I'll be your friend and so much more;
That's what a POSSLQ is for.

And everything we will confess;
Yes, even to the IRS.
Some day on what we both may earn,
Perhaps we'll file a joint return.
You'll share my pad, my taxes, joint;
You'll share my life - up to a point!
And that you'll be so glad to do,
Because you'll be my POSSLQ.



More information about the Python-list mailing list