You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In python, when updating a variable set in global scope, you need to use the global keyword, or it will give an error. In blockly, I see no way of updating a variable previously set in another scope, such as in the example.
The above blocks generate the following code:
bar = 0
def foo():
bar = bar + 1
print(bar)
foo()
Which give the following error when run:
Traceback:
File "answer.py", line 3, in foo
bar = bar + 1
File "answer.py", line 5
foo()
UnboundLocalError: UnboundLocalError: local variable "bar" referenced before assignment on line 3
The correct code generated to avoid this error and run according to the intent is:
bar = 0
def foo():
global bar
bar = bar + 1
print(bar)
foo()
The text was updated successfully, but these errors were encountered:
In python, when updating a variable set in global scope, you need to use the global keyword, or it will give an error. In blockly, I see no way of updating a variable previously set in another scope, such as in the example.
The above blocks generate the following code:
Which give the following error when run:
The correct code generated to avoid this error and run according to the intent is:
The text was updated successfully, but these errors were encountered: