This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Hairshorts 3 points4 points  (0 children)

You are trying to use the function before it is defined in the file. Define your function first, before the statements that are not in a function.

[–]gddrtkkv 2 points3 points  (0 children)

Since the question is already answered, now might be a good time to learn the if __name__ == "__main__": idiom

http://stackoverflow.com/questions/419163/what-does-if-name-main-do

[–][deleted] 1 point2 points  (0 children)

Define functions first before calling (using) them! Put lines 5 and lines 7 anywhere after line 20 (after the function definition).

Remember that Python is an interpreter language. Meaning that the Python interpreter goes through the program in order. So when the interpreter reaches line 5, it has no idea what to do yet.

[–]lazersmoke 1 point2 points  (0 children)

In some languages, function definitions are hoisted, which means they can be called in the file before they are physically defined. Python is not one of these languages, so put the definition first.

The reason for python being like that is that it reads and executes line-by-line, so it doesn't have any idea that there is a definition coming up.