Python - Problem with variable

0

I do not know where I'm going wrong, but I wrote the following line of code:

#variaveis globais
fcfs = False
#commands

def fcfscheck():

    fcfs = not fcfs

Supposedly this function should toggle the value of fcfs between true and false every time it was called, but I'm getting an error:

  

Unresolved reference 'fcfs'

    
asked by anonymous 25.11.2017 / 22:03

3 answers

1

This is a name conflict, you have a local variable with the same name as a variable declared in global scope, change the name of one of your variables and you will not have any more conflict.

And since the local variable takes precedence , then it will not be possible to get the value of fcfs because it has not yet exists in the local scope of your function, however, if I change the name of your local variable there will be no conflict, see:

#variaveis globais
fcfs = False
#commands

def fcfscheck():
  outroNome = not fcfs

And if you want to reference your global variable without changing its name, do what was suggested in the other global fcfs plies, but try to make it easier for anyone to read your code.

    
25.11.2017 / 22:34
0

Try this:

def fcfscheck():
   global fcfs
   fcfs = not fcfs

You must indicate in the function that you want to modify the global variable.

    
25.11.2017 / 22:18
0

You need to use global in the variable to use itself within a function

#variaveis globais
fcfs = False
#commands

def fcfscheck():
    global fcfs
    fcfs = not fcfs

A nice tip, I do not know which IDE you use, in my case I use the VSCode with the Pylint extension, it indicates these errors easily

    
25.11.2017 / 22:19