Function Not Changing Global Variable

Function not changing global variable

Your issue is that functions create their own namespace, which means that done within the function is a different one than done in the second example. Use global done to use the first done instead of creating a new one.

def function():
global done
for loop:
code
if not comply:
done = True

An explanation of how to use global can be found here

tkinter - Why is global variable not changed after function?

Your code does not show when foo() is being executed, however at the beginning you are already overriding the value of answer with an empty string, which later evaluates to false:

def foo():
global answer
answer = "" # Remove this line

There are many issues with this usage of global variables, and an OOP refactor would be the best approach to address it. However, a quick solution for this specific problem would be passing the result from answerMessagebox to the callback:

def answerMessagebox(returnValue, toplevel, functionName=""):
answer = returnValue
print("The answer variable is (inside answerMessagebox function): ", answer)

if functionName:
functionName(answer)
# ...

def foo(answer):
# ...

Global variable in function not updating

Because you redeclared x in your function's local scope. That's the one you assigned 10 to, instead of the global one.

Ditch the var x; and it will work.

x = 5;
var w = function(){
x = 7
// var x;
console.log(x);
x = 10;
console.log(x);
};
w();
console.log(x);

That being said, what's probably baffling you is hoisting

Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).

x = 5;
var w = function(){
x = 7
var x; // this is moved to the top of the local scope
console.log(x);
x = 10;
console.log(x);
};
w();
console.log(x);

Why is a global variable not being updated inside a function that is NOT redeclaring it as a local variable?

You can declare a function after your script and call that function when the XHR request is done and you've mutated the variable.

/* ... */
pendingPaymentTrigger = true;
pendingPayment = Math.round((obj.body.primary.payments.balances + obj.body.primary.payments.generate + obj.body.primary.payments.immature) * 100) / 100;
doSomething();
/* ... */
function doSomething() {
console.log(pendingPayment);
}

JS global variable not changing outside of function scope

Returning an object with currentPlayer only results in the object containing the value of currentPlayer at the moment of the return of the object. You need a function instead, so that you can retrieve the current value of the identifier inside on demand.

return {
init,
activePlayers,
getCurrentPlayer: () => currentPlayer,
}

and then, wherever you're using it externally, use .getCurrentPlayer() instead of .currentPlayer

Global variables not changing from one module to another

the global keyword is working only in the used file. It can not be used to make value globally from another file.



Related Topics



Leave a reply



Submit