Ask coding questions

← Back to all posts
nameerror: name is not defined
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
EugeniuszLIETZA
Dominicl645
Comments
hotnewtop
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:

Coder100
Coder100

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

Coder100

@fuzzyastrocat

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

fuzzyastrocat

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

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

Coder100

its probably closures @fuzzyastrocat

fuzzyastrocat

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

Coder100

closures are what allowed:

work
@fuzzyastrocat

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.

Coder100

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

Coder100

just a theory plz no hurt me @fuzzyastrocat

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)

Coder100

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

fuzzyastrocat
Coder100

haha I was right!! @fuzzyastrocat

fuzzyastrocat

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

Coder100
Coder100

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

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)

Coder100

:D
rust best @fuzzyastrocat

fuzzyastrocat

@Coder100 Yay, Rust is good

Whacko

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

I forked your repl, here is the working one.

Dominicl645

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

Dominicl645

give what both me and Coder100 suggested a try. @EugeniuszLIETZA

Coder100

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

EugeniuszLIETZA

ok thanks it worked

Coder100
Coder100

my question is why are you doing this lol