字典

1 phoneNumbers={"john":"555-1234"}
2 phoneNumbers=["mary"] = "555-6789"
3 phoneNumbers=["bob"] = "444-4321"
4 phoneNumbers=["jenny"] = "888-9876"
5 print phoneNumbers

python27运行后显示错误:(SyntaxError: can't assign to literal)语法错误:不能指定为字符。不知为何。

 

根据百度查询,修改代码如下:

1 phoneNumbers={"john":"555-1234","mary":"555-6789","bob":"444-4321","jenny":"888-9876"}
2 # phoneNumbers=["mary"] = "555-6789"
3 # phoneNumbers=["bob"] = "444-4321"
4 # phoneNumbers=["jenny"] = "888-9876"
5 print "mary:",phoneNumbers["mary"]
6 print phoneNumbers

显示运行正常。

mary: 555-6789

{'bob': '444-4321', 'john': '555-1234', 'mary': '555-6789', 'jenny': '888-9876'}

 

问题找见
phoneNumbers=["mary"] = "555-6789"

因改为:
phoneNumbers["mary"] = "555-6789"

 

posted on 2018-01-22 10:46  新手爱好者  阅读(152)  评论(1)    收藏  举报

导航