set a key without value for a dictionary in python -
this question has answer here:
- python function global variables? 6 answers
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
Post a Comment