Doug Howard
Posts: 9
Joined: Mon Feb 27, 2017 12:18 pm

A variable in a function call

Fri Jul 07, 2017 9:08 pm

I am not a programmer. I am trying to control ten (10) LED's and a 4 x 20 i2c LCD display in seventeen (17) different variations. I have ~ 375 lines of Python code with comments. I have inserted several prints statements and breaks to debug the code.
I can pass two string variables (Rules and Aspects) to the display in a for loop sixteen times. I have created and tested functions to turn the ten LED's on, off, and flash with PWM. In this for loop I would like to call a function with a variable. I am using "text" in the for loop with "Rules" and "Aspects" from lists successfully. So I would like to call LEDs1() through LEDs16() by item in range (1, 16) calling LEDs{item}(). I am calling LEDs(0) separately as a LED test. I have lately tried to create nested functions. Again I am not a programmer and know enough to be dangerous. I guess I need to be pointed in a direction that will work. Thank you, Doug.

User avatar
Paeryn
Posts: 2706
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: A variable in a function call

Sat Jul 08, 2017 12:40 am

Doug Howard wrote:I am not a programmer. I am trying to control ten (10) LED's and a 4 x 20 i2c LCD display in seventeen (17) different variations. I have ~ 375 lines of Python code with comments. I have inserted several prints statements and breaks to debug the code.
I can pass two string variables (Rules and Aspects) to the display in a for loop sixteen times. I have created and tested functions to turn the ten LED's on, off, and flash with PWM. In this for loop I would like to call a function with a variable. I am using "text" in the for loop with "Rules" and "Aspects" from lists successfully. So I would like to call LEDs1() through LEDs16() by item in range (1, 16) calling LEDs{item}(). I am calling LEDs(0) separately as a LED test. I have lately tried to create nested functions. Again I am not a programmer and know enough to be dangerous. I guess I need to be pointed in a direction that will work. Thank you, Doug.
Do you mean you've got something like

Code: Select all

def LEDs1():
  print("LED1")

def LEDs2():
  print("LED2")

for index in range(1, 3):
  LEDsindex()  // This won't work, just as an example of what you want to do
You could have a tuple of functions where you call the one you want...

Code: Select all

func_list = (LEDs1, LEDs2) // Tuple of functions

for index in range(1, 3):
  func_list[index-1]() // will call LEDs1() followed by LEDs2()
Or not as elegant

Code: Select all

for index in range(1, 3):
  globals()["LEDs"+str(index)]() // If the functions are global
  locals()["LEDs"+str(index)]()  // If the functions are local
She who travels light — forgot something.

Doug Howard
Posts: 9
Joined: Mon Feb 27, 2017 12:18 pm

Re: A variable in a function call

Sat Jul 08, 2017 2:22 am

Paeryn wrote:
Doug Howard wrote:I am not a programmer. I am trying to control ten (10) LED's and a 4 x 20 i2c LCD display in seventeen (17) different variations. I have ~ 375 lines of Python code with comments. I have inserted several prints statements and breaks to debug the code.
I can pass two string variables (Rules and Aspects) to the display in a for loop sixteen times. I have created and tested functions to turn the ten LED's on, off, and flash with PWM. In this for loop I would like to call a function with a variable. I am using "text" in the for loop with "Rules" and "Aspects" from lists successfully. So I would like to call LEDs1() through LEDs16() by item in range (1, 16) calling LEDs{item}(). I am calling LEDs(0) separately as a LED test. I have lately tried to create nested functions. Again I am not a programmer and know enough to be dangerous. I guess I need to be pointed in a direction that will work. Thank you, Doug.
Do you mean you've got something like

Code: Select all

def LEDs1():
  print("LED1")

def LEDs2():
  print("LED2")

for index in range(1, 3):
  LEDsindex()  // This won't work, just as an example of what you want to do
You could have a tuple of functions where you call the one you want...

Code: Select all

func_list = (LEDs1, LEDs2) // Tuple of functions

for index in range(1, 3):
  func_list[index-1]() // will call LEDs1() followed by LEDs2()
Or not as elegant

Code: Select all

for index in range(1, 3):
  globals()["LEDs"+str(index)]() // If the functions are global
  locals()["LEDs"+str(index)]()  // If the functions are local
Thank you for the reply. This will give me something to try. I understand the concept of global and local but I do not know how to tell the difference. I'm not even sure if I replied to the reply correctly.

Doug Howard
Posts: 9
Joined: Mon Feb 27, 2017 12:18 pm

Re: A variable in a function call

Sat Jul 08, 2017 6:21 am

Thank you, It worked and I still don't understand "tuples". Now I need to work on changing the PWM function to different GPIO pins. Thanks again.

User avatar
Paeryn
Posts: 2706
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: A variable in a function call

Sat Jul 08, 2017 8:50 am

Doug Howard wrote:Thank you, It worked and I still don't understand "tuples". Now I need to work on changing the PWM function to different GPIO pins. Thanks again.
A tuple is just a sequence of items similar to a normal list that you'd make with [LEDs1, LEDs2] but tuples are immutable so you can't add or remove items from them, nor can you change an element. Otherwise you use them the same way.
The only difference is if you want to create a tuple with one item, you have to put a comma after the item otherwise the brackets are taken as normal evaluation parentheses, so a one element tuple would be (4,)
She who travels light — forgot something.

Return to “Beginners”