set a key without value for a dictionary in python -


this question has answer here:

i trying set dictionary staff's salary in python. want create 2 functions, getname() , getsalary() tried:

empdic={}  def setname(n):  empdic={'n':''} 

and in python interactive 2.7 typed:

>>>setname('marson')  >>>print empdic  {} 

the result still empty dictionary, how can deal that?

you need modify original dict:

empdic={}  def setname(d, n):    d[n] = ""  setname(empdic,"marson")  print(empdic) {'marson': ''} 

you creating new local variable empdic in function, using string "n" not name n passed in function.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -