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 'u1' is not defined (on parser code Python) Melcu54 1 150 Jul-26-2021, 04:36 PM
Last Post: snippsat
  I am getting a NameError that is not defined and not sure why it happen rick0922 5 396 Jun-14-2021, 03:41 PM
Last Post: deanhystad
  [split] NameError: name 'start_with_first_selected' is not defined knoxvilles_joker 4 600 Apr-18-2021, 07:59 PM
Last Post: knoxvilles_joker
  “NameError” “is not defined” script that I know works on other computers iamwithstupid 0 585 Nov-24-2020, 04:23 PM
Last Post: iamwithstupid
  NameError: name 'L2' is not defined soshelby 5 995 Nov-05-2020, 08:50 PM
Last Post: soshelby
  NameError: name 'target_id' is not defined bhagyashree 7 909 Nov-03-2020, 10:22 AM
Last Post: bhagyashree
  code not working, NameError: name 's' is not defined ridgerunnersjw 4 1,038 Oct-05-2020, 07:03 PM
Last Post: buran
  Going thru tutorials..."NameError: name 'raw_input' is not defined" hmonnier 4 1,164 Jul-14-2020, 02:19 PM
Last Post: BitPythoner
  python library not defined in user defined function johnEmScott 2 864 May-30-2020, 04:14 AM
Last Post: DT2000
  [split] NameError: name 'formats' is not defined shobhu123 1 1,097 May-24-2020, 06:29 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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