You have to define functions before you can call them. Function definitions in Python are executable statements like any other, and are executed in the order they occur in the source file. Move your def fun block up ahead of the place where you call it.
Looks to me like the interpreter is running the the line of code calling the function before it is defining the function. You need to switch them around, or else wrap the function call code itself in a function like main() and call that after everything is defined.