本人接触python十天左右,这是我写出的几行代码
dms = {
'zhang': {
'nickname': 'zwc',
'school': 'zx',
},
'feng': {
'nickname': 'fcl',
'school': 'zx',
},
'zyh': {
'nickname': 'asam',
'school': '47'
},
for name, member_info in dms.items():
a = member_info['nickname']
b = member_info['school']
print(name + ", aka " + a + " studies at " + b)
出错的信息是
for name, member_info in dms.items():
syntaxerror: invalid syntax
这里书上也没有仔细讲,我百思不得其解所以提问
dms = { 'zhang': { 'nickname': 'zwc', 'school': 'zx', }, 'feng': { 'nickname': 'fcl', 'school': 'zx', }, 'zyh': { 'nickname': 'asam', 'school': '47' } } for name, member_info in dms.items(): a = member_info['nickname'] b = member_info['school'] print(name + ", aka " + a + " studies at " + b)
你的语法有点错,dms少了一个反大括号
第二种循环方法:
while dms: k, v = dms.popitem() print(k, v['nickname'], v['school'])
dms = {
'zhang': {
'nickname': 'zwc',
'school': 'zx',
},
'feng': {
'nickname': 'fcl',
'school': 'zx',
},
'zyh': {
'nickname': 'asam',
'school': '47'
}
},
for name, member_info in dms.items():
他提示‘tuple object has no attribute 'items'
@skrrr: 你的dms结尾的大括号多了一个逗号,你把我的代码复制去执行就可以了。
@抽象ID: 多謝,解決了我多個問題