Python Forum
NameError: name 'pins' is not defined - 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: NameError: name 'pins' is not defined (/thread-27650.html)



NameError: name 'pins' is not defined - glennford49 - Jun-15-2020

NameError: name 'pins' is not defined

result_names = 'elena'
dict={"glenn":1,"elena":2}
def person(names):
	pins =dict.get(names)
	return pins
    
person(result_names)
print(pins)
im trying to get the value of a specific key, but it returns this error


RE: return a function error - buran - Jun-15-2020

result_names = 'elena'
dict={"glenn":1,"elena":2}
def person(names):
    pins =dict.get(names)
    return pins
     
pins = person(result_names)
print(pins)
you need to assign the value returned from the function to a name (unless you use it directly and not plan to use it again)
by the way, don't use dict as variable name - it's a build in function.
Also, you use it inside the function (as global name). It's better either have it inside the function or pass it as argument like you do with names (in which case there is the question do you need the function at all)

name = 'elena'

def get_pin(name):
    """ Given a name, return pin

    """
    users = {"glenn":1, "elena":2}
    pin = users.get(name)
    return pin # you can replace this line and the previous with return users.get(name)
      
pin = get_pin(name)
print(pin)



RE: return a function error - glennford49 - Jun-15-2020

(Jun-15-2020, 12:00 PM)buran Wrote:
result_names = 'elena'
dict={"glenn":1,"elena":2}
def person(names):
    pins =dict.get(names)
    return pins
     
pins = person(result_names)
print(pins)
you need to assign the value returned from the function to a name (unless you use it directly and not plan to use it again)
by the way, don't use dict as variable name - it's a build in function.
Also, you use it inside the function (as global name). It's better either have it inside the function or pass it as argument like you do with names (in which case there is the question do you need the function at all)

name = 'elena'

def get_pin(name):
    """ Given a name, return pin

    """
    users = {"glenn":1, "elena":2}
    pin = users.get(name)
    return pin # you can replace this line and the previous with return users.get(name)
      
pin = get_pin(name)
print(pin)

thanks... it works like a charm +1 for you