Python Forum
NameError: name 'pins' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'pins' is not defined
#1
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
Reply
#2
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: name 'pi' is not defined katebishop 2 2,556 Jul-15-2024, 05:48 AM
Last Post: Pedroski55
  I'm getting a NameError: ...not defined. vonArre 2 4,669 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 2,114 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 5,277 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 5,600 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 2,412 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 3,048 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 16,156 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 20,550 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 4,891 Aug-15-2021, 06:12 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020