标签:直接 使用 iter ring not def rip 对象 table
学生管理系统:
all_info = {‘UZI‘:{‘name‘:‘UZI‘,‘age‘:‘18‘,‘tel‘:‘9199999199‘},
‘xiye‘:{‘name‘:‘xiye‘,‘age‘:‘20‘,‘tel‘:‘37402384702‘}}
new_infor = {‘name‘:‘UZI‘,‘age‘:‘18‘,‘tel‘:‘9199999199‘,
‘name‘:‘xiye‘,‘age‘:‘20‘,‘tel‘:‘37402384702‘}
def zj_fun ():
new_name = input(‘请输入你的名字:‘)
if new_name in all_info.keys():
print(‘你的输入有误!‘)
else:
new_age = input(‘请输入你的年龄:‘)
new_tel = input(‘请输入你的联系方式‘)
new_infor [‘name‘] = new_name
new_infor [‘age‘] = new_age
new_infor [‘tel‘] = new_tel
all_info [new_name] = new_infor
print(all_info)
def sc_fun ():
new_name = input(‘请输入你的名字:‘)
if new_name in all_info.keys():
all_info.pop(new_name)
else:
print(‘你的输入有误!‘)
print(all_info)
def g_fun ():
new_name = input(‘请输入你的名字:‘)
if new_name in all_info.keys():
new_age = input(‘请输入你的年龄:‘)
new_tel = input(‘请输入你的联系方式‘)
all_info[new_name][‘age‘] = new_age
all_info[new_name][‘tel‘] = new_tel
else:
print(‘你的输入有误!‘)
print(all_info)
def cx_fun():
for k,v in all_info.items():
print(‘姓名:%s‘ % k)
print(‘年龄:%s‘ % v[‘age‘])
print(‘联系方式:%s‘ % v[‘tel‘])
zj_fun()
遇到的几个小问题:
in 可以直接查询对象是否在字典的key里。不用循环遍历每个key.
在字典里,遍历和更改同时使用,容易报错。dictionary changed size during iteration
‘method‘ object is not subscriptable 意味代码里有括号的使用错误。把 [] 改为 ()
标签:直接 使用 iter ring not def rip 对象 table
原文地址:https://www.cnblogs.com/ppzhang/p/8998977.html