字典dict的用法
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
name='zsy'
password = '123'
dictKV = {'zsy':'123','aaa':'456','bbb':'789'}
a = dictKV.items()
print(".item=",a) #.item= dict_items([('zsy', '123'), ('aaa', '456'), ('bbb', '789')])
b = dictKV.keys()
print(".keys=",b) #.keys= dict_keys(['zsy', 'aaa', 'bbb'])
c = dictKV.values()
print(".values=",c) #.values= dict_values(['123', '456', '789'])
if name in b:
print('ok') #ok
if password == dictKV[name]:
print('yes') #yes
ar=[1,2,3,4,5]
d = dict.fromkeys(ar) #d= {1: None, 2: None, 3: None, 4: None, 5: None}
d1 = dict.fromkeys([1,2,3],'zsy') #d1= {1: 'zsy', 2: 'zsy', 3: 'zsy'}
d2 = dict.fromkeys(dictKV,'zzz') #d2= {'zsy': 'zzz', 'aaa': 'zzz', 'bbb': 'zzz'}
d3 = dict.fromkeys(ar,'[one,two]') #d3= {1: '[one,two]', 2: '[one,two]', 3: '[one,two]', 4: '[one,two]', 5: '[one,two]'}
dictKV.clear() # 清空词典所有条目
print(dictKV) #{}
del dictKV # 删除词典
print(dictKV) #NameError: name 'dictKV' is not defined
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
dictKV = {'zsy':'123','aaa':'456','bbb':'789'}
def login():
global dictKV
print("|---用户注册:N/n---|")
print("|---账号登录:E/e---|")
print("|---退出登录:Q/q---|")
inwhat = input("|---请输入对应指令---|")
if inwhat=='N' or inwhat=='n':
account = input("请输入用户名:")
while account in dictKV:
account=input("此用户名已经被使用,请重新输入")
password = input("请输入密码:")
dictKV[account]=password
print("注册成功")
elif inwhat=='E' or inwhat=='e':
account = input('请输入用户名: ')
while account not in dictKV:
account = input("您输入的用户名不存在,请重新输入:")
password = input('请输入密码: ')
if(password ==dictKV[account]):
print('Login successfully ')
else:
print('Login failed, password is wrong')
elif inwhat=='Q' or inwhat=='q':
print('Logout')
return
if __name__ == '__main__':
login()