6

I have these two functions:

def check_channel_number(self):
    print "***************Channel Checker *********************" 
    print ''
    user_channel_number = int(re.sub('\D', '', raw_input("Enter a channel number, (3digit): "))[:3]);
    channel = ("channelNr= '%d'") % (user_channel_number)
    print channel
    # channel_search = channel + str(user_channel_number)
    datafile = file('output.txt')

    found = False
    for line in datafile:
        if channel in line:            
            found = True 
            print 'The channel number you entered is correct and will be deleted'
            return user_channel_number

    print 'The channel number you entered is not on the planner'
    return False

and

def delete_events(self):
    if user_channel_number == True:
        return 'The program number is correct and will be deleted'

        # action = 'DeleteEvent'
        menu_action = 'all'
        book = 'RECYC:687869882'
        arg_list = [('C:\\Users\\yke01\\Documents\\StormTest\\Scripts\\Completed'
                        '\\Utils\\UPNP_Client_Cmd_Line.py')]
        arg_list.append(' --action=')
        arg_list.append(action)
        arg_list.append(' --ip=')
        arg_list.append('10.10.8.89')
        arg_list.append(' --objectId=')
        arg_list.append(book)
        subprocess.call(["python", arg_list])

        print 'The program deleted successfully'

When I run my script, it says that user_channel_number is not defined globally. How can I use user_channel_number inside the delete_events function?

1
  • 1. You typecasted user_channel_number to int, then try to compare it to True. 2. check_channel_number always returns false. 3. Your return statement in delete_events stops all of the code after that from running. It looks like you're in a class? You can define self.user_channel_number to make it accessible for all methods in the class that you're in. Commented Nov 25, 2016 at 9:44

3 Answers 3

11

Functions can not share their local variables. You can return the value from the first and pass it to the second:

def check_channel_number(self):
    ...
    return user_channel_number

def delete_events(self):
    user_channel_number = self.check_channel_number()
    ...

Or save value on the object:

def check_channel_number(self):
    ...
    self.user_channel_number = user_channel_number
    ...

def delete_events(self):
    if self.user_channel_number:
         ....
Sign up to request clarification or add additional context in comments.

Comments

11

When you define a variable inside of a function, it is a local variable, meaning that it can only be accessed within that function.

Within a Class

It looks like you're inside a class, so you can make the variable accessible to all methods in the class by defining it like this:

def check_channel_number(self):

    self.user_channel_number = ...

And then in your second function, you can use it like the following:

def delete_events(self):

    if self.user_channel_number:

Outside of a class

If you aren't using methods inside of a class, you can instead use the global builtin.

For example,

def check_channel_number():

    global user_channel_number 
    user_channel_number = ...

def delete_events():

    if user_channel_number:

        ...

Using a value returned from a function

Instead in your first function, check_channel_number(), you can have it return user_channel_number. You can then call that function inside of delete_events(), like the following:

def check_channel_number():

    user_channel_number = ...
    return user_channel_number

def delete_events():

    if check_channel_number():

        ...

Comments

1

So I think when you call the check_channel_number function, user_channel_number is defined in there, so when you call delete_events, it has gone out of scope, maybe something like this would help?

user_channel_number = check_channel_number()
delete_events()

I'd probably have the user_channel_number as an input to the delete function too, so it would turn into this: (where ucn is the user_channel_number)

def delete_events(self, ucn):
    if ucn == True:
        print 'The program number is correct and will be deleted'

        # action = 'DeleteEvent'
        menu_action = 'all'
        book = 'RECYC:687869882'
        arg_list = [('C:\\Users\\yke01\\Documents\\StormTest\\Scripts\\Completed'
                    '\\Utils\\UPNP_Client_Cmd_Line.py')]
        arg_list.append(' --action=')
        arg_list.append(action)
        arg_list.append(' --ip=')
        arg_list.append('10.10.8.89')
        arg_list.append(' --objectId=')
        arg_list.append(book)

        subprocess.call(["python", arg_list])

        print 'The program deleted successfully'

I have also changed `return 'The program number is correct and will be deleted'' to a print statement as I have a feeling the return would end the function before the other lines of code would be run

So the code would probably end up being something like:

user_channel_number = check_channel_number()
delete_events(user_channel_number)

EDIT:

just noticed it looks like your functions are part of a class,

in that case, you could do:

self.ucn = self.check_channel_number()
self.delete_events(self.ucn)

(or if you dont want to pass the user_channel_number into the function you could change if user_channel_number: to if self. user_channel_number:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.