I'm a beginner in Python and have a problem. I can't add the value of a variable to a dictionary.
fe
name=input('Input the name: ')
dict['Name']=name
and here's the error:
TypeError: 'type' object does not support item assignment
Could anyone help me?
Don't name your dictionary dict
. That's a built-in. Name it something else, and you example should work, provided the dict object is initialized.
my_dict = {}
name=input('Input the name: ')
my_dict['Name']=name
You have to instantiate the dictionary before:
d = dict()
d['Name'] = name
Given your snippet, i believe you named your dictionnary dict
, which is a python built-in name. You should use another name for your variable ( always avoid using built-in names for variables ) like:
d = {}
d['Name'] = name
You may either
d = {}
d["Name"] = "yourName"
or
d = {"Name": "yourName"}
both would work
你可能想做
dict(Name = name)
您也可以使用以下代码:
d = dict(Name=input('Input the name: '))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.