dam1ne
dam1ne

Reputation: 361

how to call global variables within the same function

I want to use the global variables in and outside of the function, but there is an error with calling usingDesired in the print statements

def getTargetPage():

    global usingDesired
    global usingNotChecked

    while True:

        desiredPages = open(r'C:\Users\tree3\Desktop\Instagram\desired_pages.txt', "r")
        pagesNotChecked = open(r'C:\Users\tree3\Desktop\Instagram\pages_not_checked.txt', "r")

        if os.path.getsize(r'C:\Users\tree3\Desktop\Instagram\pages_not_checked.txt') == 0:
            fileReader = open(r'C:\Users\tree3\Desktop\Instagram\desired_pages.txt', "r")
            usingDesired = True

        if os.path.getsize(r'C:\Users\tree3\Desktop\Instagram\pages_not_checked.txt') > 0:
            fileReader = open(r'C:\Users\tree3\Desktop\Instagram\pages_not_checked.txt', "r")
            usingNotChecked = True

        print(usingDesired)
        print(usingNotChecked)

Upvotes: 0

Views: 49

Answers (1)

Shamim
Shamim

Reputation: 69

declare a variable usingDesired globally without global keyword. Inside function you can manipulate this variable as below

global usingDesired
usingDesired = 'udpate global variable value'

Upvotes: 1

Related Questions