Open IDLE and start a new editor window. In the editor window, define a function named
add() that returns the sum of its two parameters.
def add(x, y): and then on the next line, indented,
return x + y.
Just like you learned a moment ago,
adder.py is a Python module. So far so good. Now open another editor window and type the following code:
value = add(2, 2) and then on the next line,
NameError occurs because
add() is defined in
adder.py and not in
main.py. That’s the file you ran. So if you think a moment, then this error makes sense. How should Python know that you mean the
add() function from the
Become a Member to join the conversation.