local-variables

Strange behaviour of the program after modifying the dictionary of local variables

Strange behaviour of the program after modifying the dictionary of local variables Question: I find some strange behavior in my program after modifying the dictionary of local variables, and it makes me confused. If we execute the code below, we will get the result as the image I attached. And now I know I shouldn’t …

Total answers: 1

Local variable 'gamebot' referenced before assignment

Local variable 'gamebot' referenced before assignment Question: This is my question here:) My question is straight forward, the first program is the correct program but second program isnt, why ? gamebot = "OFF" def gamebot_status(): if gamebot == "OFF": gamebotTest = gamebot print("Gamebot is right now ", gamebotTest, "nTurning On gamebot in… n1 n2 n3 …

Total answers: 1

python function global and local scope confusion

python function global and local scope confusion Question: I have a code in which I declare a variable globally. Then inside a function, when I try to use it, it gives an error Unbound variable is not declared My code: count_url =1 def foo(): … ttk.Label(canvas1, text=f'{varSongTitle}…Done! {count_url}/{str(var_len)}’).pack(padx=3,pady=3) root.update() count_url = count_url + 1 When …

Total answers: 1

`eval('bar')` causes a `NameError`, but `print(bar); eval('bar')` doesn't

`eval('bar')` causes a `NameError`, but `print(bar); eval('bar')` doesn't Question: The following code 1 def foo(): 2 def bar(): 3 return ‘blah’ 4 def baz(): 5 eval(‘bar’) 6 baz() 7 foo() causes a NameError — the entire error message is Traceback (most recent call last): File "main.py", line 7, in <module> foo() File "main.py", line 6, …

Total answers: 1

Changing local variable using recursion

Changing local variable using recursion Question: I am using a recursive function implemented in a python class. Am I right that the local variable (passed through the methods attributes) isn’t changing through a recursive change? I have the following tree: example tree My current method is the following: def readSequence(self, linkTable, partTable, seq, element): #clearFlags …

Total answers: 1

Python – local variable is assigned but never used – var=None

Python – local variable is assigned but never used – var=None Question: In an attempt to format my code a little better to avoid redundancies in multiple methods doing the same stuff for different classes, I’m faced with the following problem : # problematic method def a(self, b, c): result = test(b) if (result): c …

Total answers: 2

Local variable might be referenced before assignment

Local variable might be referenced before assignment Question: I’ve been trying to make an encryption and decryption system but I have run into a small error. Here is my code: import sys import pyperclip def copy(data): question = input("Copy to clipboard? ") if question.lower() == ‘yes’ or question.lower() == ‘y’: pyperclip.copy(data) print("Encrypted message copied to …

Total answers: 3

Accessing function-local variable

Accessing function-local variable Question: This code def reportRealDiagnostics(): ranks = 0 class Rank: def __init__(self): global ranks ranks += 1 rank = Rank() reportRealDiagnostics() produces NameError: global name ‘ranks’ is not defined I am sure that this is all what you need to answer the question. Asked By: Val || Source Answers: You should use …

Total answers: 2

Access a function variable outside the function without using "global"

Access a function variable outside the function without using "global" Question: I am trying to access a local function variable outside the function in Python. I can make code like this work with global variables: bye = ” def hi(): global bye bye = 5 sigh = 10 hi() print(bye) Next, I tried this code, …

Total answers: 6