Python Forum
Name error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Name error (/thread-28948.html)



Name error - Sky737 - Aug-11-2020

Hi I am new to python and can't figure out why my code is not working. Here is my code...

def packet_1(packet_1_LOC, packet_1_name):
    packet_1_name = "File"
    packet_1_LOC = "Desktop"
    print(packet_1_LOC)
    print(packet_1_name)
    return()
packet_1(packet_1_name,packet_1_LOC)
Here is the error message...
Error:
packet_1(packet_1_name,packet_1_LOC) NameError: name 'packet_1_name' is not defined
Please let me know if you know why it's not working.

Thanks, Connor


RE: Name error - bowlofred - Aug-11-2020

Please put your code inside "python" tags so the formatting can be seen.

Variables assigned inside a function are by default only available within the function. When you call packet_1, it will take the first argument and assign that to packet_1_LOC in the function. Then you assign "Desktop" to it, discarding anything that was assigned previously.

In your case, the first argument is "packet_1_name", but that variable has never been assigned outside a function, so there is no value that can be sent to the function.