Skip to content
Sign upLog in
← Back to Community
nameerror: name is not defined
Profile icon
EugeniuszLIETZA

the hello() function causes and error becuase 'string' is not defined, although it clearly is defined.

why does this happen and how do i fix it

Answered by fuzzyastrocat [earned 5 cycles]
View Answer
Voters
Profile icon
MaxXMaiSan
Profile icon
JordiPerez3
Profile icon
EugeniuszLIETZA
Profile icon
Dominicl645
Comments
hotnewtop
Profile icon
fuzzyastrocat

although it clearly is defined

Actually no, to Python it's not (I know, weird right?). Python requires that you "import" the string into the scope of the function with global. (EDIT: There's something weird going on... this shouldn't have to be done with Python, but for some reason it's the only thing that works.).

So, you'll want to do this:

string = "hello world" def hello(): global string print(string) hello()
Profile icon
Coder100
Profile icon
Coder100

hm it still is, that would be completely valid in python

@fuzzyastrocat

Profile icon
Coder100

@fuzzyastrocat

string = "hello world" def hello(): print(string) hello()

works fine because of closures, but in exec apparently this isn't the case?

Profile icon
fuzzyastrocat

@Coder100
Actually this is weird, that works fine within an exec for me. Not sure what the OP's problem was now.

Profile icon
fuzzyastrocat

@Coder100
Ok, it appears to only occur because the exec is inside a function. I'm actually really not sure why this is happening

Profile icon
Coder100

its probably closures

@fuzzyastrocat

Profile icon
fuzzyastrocat

@Coder100
That's very vague... what do you mean?

Profile icon
Coder100

closures are what allowed:

a = "" def something(): print(a)

work


@fuzzyastrocat

Profile icon
fuzzyastrocat

@Coder100
I understand what closures are (I'm a functional programmer, if I didn't I would be trash :D), but what I mean is that I don't understand what you mean by "closures" (a very broad topic) being the root of the problem.

Profile icon
Coder100

@fuzzyastrocat
I think python doesn't automatically bind the code in exec

Profile icon
Coder100

just a theory plz no hurt me

@fuzzyastrocat

Profile icon
fuzzyastrocat

@Coder100
Perhaps Python does behave differently inside an exec. (What I find odd is that it works fine when you put the exec outside of the "call" function, but it doesn't when it's inside.)

(Lol I'm not trying to hurt you, sorry if I come off as being mad :D)

Profile icon
Coder100

lol its fine
anyways yeah exec does have something about being able to define variables...

@fuzzyastrocat

Profile icon
fuzzyastrocat
Profile icon
Coder100

haha I was right!!

@fuzzyastrocat

Profile icon
fuzzyastrocat

@Coder100
Good job! And I've learned another one of Python's quirks.

Profile icon
Coder100
Profile icon
Coder100

yeah python is bad use ruby it's literally the same but superior

@fuzzyastrocat

Profile icon
fuzzyastrocat

@Coder100
I would agree that if I had to choose between Python or Ruby I'd choose Ruby. (But I'm really not a fan of either :D)

Profile icon
Coder100

:D
rust best

@fuzzyastrocat

Profile icon
fuzzyastrocat

@Coder100
Yay, Rust is good

Profile icon
Whacko

folder/hello world.py is invalid.
I don't know why. But here's one way it works:

def hello(): string = "hello world" print(string) hello()

I forked your repl, here is the working one.

Profile icon
Dominicl645

it said string isn't defined, so you need to do something like: string = "hello world" then put in print(string)

Profile icon
Dominicl645

give what both me and Coder100 suggested a try.

@EugeniuszLIETZA

Profile icon
Coder100

something about globals and exec being bad, ok change your hello world.py to:

string = "hello world" def hello(): global string print(string) hello()
Profile icon
EugeniuszLIETZA

ok thanks it worked

Profile icon
Coder100
Profile icon
Coder100

my question is why are you doing this lol