Add variable to function in Python

I need to add the variable var2 to a function:

This does not work:
var2 = pin25
var2+.start(100)

Open in new window


This does work:
pin25.start(100)

Open in new window

Zeke RichOwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Flabio GatesCommented:
so why not just use
var1.start(100)

Open in new window

what did you expect to accomplish with the plus (+) sign?
0
Zeke RichOwnerAuthor Commented:
Flabio I have only been programming in Python for 1 week so im not really sure what i am doing. When i try var2 it does not do anything and does not print also.

This is my working code. it works perfect with pin25.start(100) but when i try to change that to var2 it no longer works:

#!/usr/bin/python
 
import RPi.GPIO as GPIO        #This line alone caused 90 minutes of frustration
import sys
import time
import fcntl  #To lock this script

#Begin to lock the file
pid_file = 'LOCKFILE-FOR-THIS-SCRIPT.pid'
fh = open(pid_file, 'w')

try:
    fcntl.lockf(fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
    # another instance is running
    print ('Error: Another instance is running...')
    sys.exit(0)

# FOLLOWING IS THE NORMAL CODE 
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
 
GPIO.setup(23, GPIO.OUT) #set pin 21 to output
GPIO.setup(24, GPIO.OUT) #set pin 21 to output
GPIO.setup(25, GPIO.OUT) #set pin 21 to output
 
pin23 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin24 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin25 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
 
pin23.start(0) #start the pwm at 0 percent power
pin24.start(0) #start the pwm at 0 percent power
pin25.start(0) #start the pwm at 0 percent power
 
var1 = sys.argv[1].strip() #get the on/off variable sent from php
var2 = sys.argv[2].strip() #get the GPIO Pin variable sent from php on/off
 
print('Received from variable 1: "%s"' % var1)
print('Received from variable 2: "%s"' % var2)

 
if var1 == 'on':
    pin25.start(100)
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic
    print("was turned on")
elif var1 == 'off':
    pin25.stop()
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic
    print("was turned off")
else:
    print("unknown command")

Open in new window

0
Zeke RichOwnerAuthor Commented:
I am now getting this error:
 sudo python /home/pi/Desktop/light.py on 25
Received from variable 1: "on"
Received from variable 2: "25"
Traceback (most recent call last):
  File "/home/pi/Desktop/light.py", line 44, in <module>
    var2.start(100)
AttributeError: 'str' object has no attribute 'start'

Open in new window


From This script using var2:
#!/usr/bin/python
 
import RPi.GPIO as GPIO        #This line alone caused 90 minutes of frustration
import sys
import time
import fcntl  #To lock this script

#Begin to lock the file
pid_file = 'LOCKFILE-FOR-THIS-SCRIPT.pid'
fh = open(pid_file, 'w')

try:
    fcntl.lockf(fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
    # another instance is running
    print ('Error: Another instance is running...')
    sys.exit(0)

# FOLLOWING IS THE NORMAL CODE 
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
 
GPIO.setup(23, GPIO.OUT) #set pin 21 to output
GPIO.setup(24, GPIO.OUT) #set pin 21 to output
GPIO.setup(25, GPIO.OUT) #set pin 21 to output
 
pin23 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin24 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin25 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
 
pin23.start(0) #start the pwm at 0 percent power
pin24.start(0) #start the pwm at 0 percent power
pin25.start(0) #start the pwm at 0 percent power
 
var1 = sys.argv[1].strip() #get the on/off variable sent from php
var2 = sys.argv[2].strip() #get the GPIO Pin variable sent from php on/off
 
print('Received from variable 1: "%s"' % var1)
print('Received from variable 2: "%s"' % var2)

 
if var1 == 'on':
    var2.start(100)
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic
    print("was turned on")
elif var1 == 'off':
    var2.stop()
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic
    print("was turned off")
else:
    print("unknown command")
	

Open in new window

0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Flabio GatesCommented:
ok, I think I understand what you are trying to do so try something like:
#!/usr/bin/python
 
import RPi.GPIO as GPIO        #This line alone caused 90 minutes of frustration
import sys
import time
import fcntl  #To lock this script

#Begin to lock the file
pid_file = 'LOCKFILE-FOR-THIS-SCRIPT.pid'
fh = open(pid_file, 'w')

try:
    fcntl.lockf(fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
    # another instance is running
    print ('Error: Another instance is running...')
    sys.exit(0)

# FOLLOWING IS THE NORMAL CODE 
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
 
GPIO.setup(23, GPIO.OUT) #set pin 21 to output
GPIO.setup(24, GPIO.OUT) #set pin 21 to output
GPIO.setup(25, GPIO.OUT) #set pin 21 to output
 
pin23 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin24 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin25 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
 
LOOKUP = {
    "pin23": pin23,
    "pin24": pin24,
    "pin25": pin25
    } 
 
pin23.start(0) #start the pwm at 0 percent power
pin24.start(0) #start the pwm at 0 percent power
pin25.start(0) #start the pwm at 0 percent power
 
var1 = sys.argv[1].strip() #get the on/off variable sent from php
var2 = sys.argv[2].strip() #get the GPIO Pin variable sent from php on/off
 
print('Received from variable 1: "%s"' % var1)
print('Received from variable 2: "%s"' % var2)
 
if var1 == 'on':
    LOOKUP[var2].start(100)
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic
    print("was turned on")
elif var1 == 'off':
    LOOKUP[var2].stop()
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic
    print("was turned off")
else:
    print("unknown command")

Open in new window

where the important parts are
LOOKUP = {
    "pin23": pin23,
    "pin24": pin24,
    "pin25": pin25
    } 

Open in new window

and
    LOOKUP[var2].start(100)

Open in new window

and
    LOOKUP[var2].start(100)

Open in new window


Basically what I did was create a dictionary mapping the string values in var2 to the actual pin objects.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Flabio GatesCommented:
oh and in your original post, the following
var2 = pin25
was misleading because var2 is not equal to pin25 but is instead equal to the string "pin25"
the difference is crucial
0
Zeke RichOwnerAuthor Commented:
Exactly the solution i needed!!! Thank you, you saved the day!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.