Python Forum

Full Version: NameError: name 'pins' is not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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)
(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