Ask coding questions

← Back to all posts
nameerror: name is not defined
EugeniuszLIETZA (1)

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 (1859) [earned 5 cycles]
View Answer
Comments
hotnewtop
fuzzyastrocat (1859)

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()
Coder100 (18071)

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

Coder100 (18071)

@fuzzyastrocat

string = "hello world"

def hello():
  print(string)

hello()

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

fuzzyastrocat (1859)

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

fuzzyastrocat (1859)

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

fuzzyastrocat (1859)

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

Coder100 (18071)

closures are what allowed:

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

work
@fuzzyastrocat

fuzzyastrocat (1859)

@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 (18071)

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

Coder100 (18071)

just a theory plz no hurt me @fuzzyastrocat

fuzzyastrocat (1859)

@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 (18071)

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

fuzzyastrocat (1859)

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

Coder100 (18071)

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

fuzzyastrocat (1859)

@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)

Whacko (323)

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.

Dominicl645 (429)

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

Dominicl645 (429)

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

Coder100 (18071)

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

string = "hello world"

def hello():
  global string
  print(string)

hello()
Coder100 (18071)

my question is why are you doing this lol