Code Saying Something Is Undefined

Question:
I am trying to make a calculator thing for fun. Yet I am getting this odd error that says ‘f’ is not defined when ‘f’ is defined right onto it. Does anyone have an idea to fix it?
Repl link:
https://replit.com/@Chandler0Bing/My-Crapy-Calulator#main.py

code snippet

that’s because the variable f is out of the scope of your print statement. You either need to declare f in scope with you print statement or move your print statement into the scope of f.

I’ll explain scope below

1 Like

Scope in python basically works like this.

Any time I have a variable, it has a scope, which is you can think of as the range where it can be accessed. Any code in that range can access and change my variable.

A variable like this

myint = 123

Can be access basically anywhere in my code. Except inside of a function, since functions have their own scope. I’ll get to that later.

So this code will print myint:

myint = 123

if myint == 123:
  print(f"Your Int: {myint}")

But say for example, I declare myint inside of an if statement, like this:

if 1 == 1:
  myint = 123

Then the variable myint’s scope is only inside of that if statement. If I try and access it outside of the if statement, like this:

if 1 == 1:
  myint = 123

print(myint)

I’ll get an error that myint is undefined, because it’s scope is only inside that if statement.

To fix this, I need to declare it outside of the if statement, and then I can access it.

myint = 0

if 1 == 1:
  myint = 123

print(myint)

Now I mentioned earlier that Functions have their own scope. A variable declared outside of a function won’t be able to be accessed inside of a function unless you declare it as global.

This won’t work:

myint = 123

def print_myint():
  print(myint)

If I run that code I’ll get an error saying I might be trying to access a local variable.
to make myint available inside the scope of my function, I can declare it as a global variable

myint = 123

def print_myint():
  global myint
  print(myint)

That code above will work

Hopefully this makes it clear, if it doesn’t, just ask and I’ll try and explain it better.

2 Likes

I dont know if you would know why this is happening but when I go to that section of the replit again, it lets me do it and spits a number back at me with it. But if I do any other section it will give me the error. Would your thing work if I put that for it, would you think?

That works, (I tested it) as you can usually get any variable from a scope above but not below.

The global keyword is dangerous because of how the GC affects it, it is recommended never to use global vars. (In my experience)

It wasn’t indented. :frowning_face:

Ah, always the indentation XD

:cry: That was me when I saw why :angry: Also some of that

Yes because myint is declared outside of any indentation it should be available to any function, but you won’t be able to change the value (I’m going to test this I might be wrong). The reason you can’t access a variable below the function is usually because python executes the code line by line meaning a variable below some code hasn’t been declared yet.

I didn’t know that about using global, I’ve used it tons of stuff and never had any issues (or just didn’t notice them) I’ll have to look into that.

Try running this code:



int1 = 123

int2 = 123



def change1():
	int1 = 321

def change2():
	global int2
	int2 = 321


change1()
change2()

print(f"Int 1: {int1}")
print(f"Int 2: {int2}")

the value of int2 will be changed, the value of int1 will only be changed inside the scope of the function. So outside of the function the value will stay the same.

1 Like

First of all, try to avoid global variables as much as possible.
Constants are ok.

But if you truly need to, you need the global keyword or it will not work, just try the code somebody just posted before me and you will see.

It can result in memory leaks because it is telling the garbage collector to never clean it up until the program ends, but in most small cases it is ok.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.