1.8.5
''':cvar
字典:
特点:
1.符号 {}
2.关键字:dict
3。保存元素是:key:value 一对
'''
# 定义
dict1 = {} # 空字典
dict2 = dict() # 空字典
dict3 = {"ID": "123156489795", "name": "lucky", "age": 18}
dict4 = dict([('name', 'lucky'), ('age', 18)])
print(dict4)
# 注意:list可以转成字典,但是前提:列表中元素都要成对出现
# 字典的增删改查:
# 增加:
dict6 = {}
# dict6[key]=value
# 特点:按照上面的格式,如果在字典中存在同名的key,则发生值覆盖(后面的值覆盖原来的)
# 如果没有同名的key,则实现的是添加功能(key:value添加到字典中)
dict6["brand"] = "huawei"
print(dict6)
dict6["brand"] = "xiaomi"
print(dict6)
dict6['type'] = 'p30 pro'
dict6['price'] = 9000
dict6['color'] = '黑色'
print(dict6)
'''
案例:
用户注册功能
username
password
email
phone
'''
print("---------欢迎来到智联招聘用户注册--------------")
# 模拟数据库
database=[]
while True:
username = input("输入用户名")
password = input("输入密码:")
repassword = input("输入确认密码")
email = input("输入邮箱")
phone = input("输入手机号:")
# 定义一个字典
user = {}
# 将信息保存到字典中
user['username'] = username
if password == repassword:
user['password'] = password
else:
print("两次密码不一致,重新输入")
continue
user['email'] = email
user['phone'] = phone
# 保存到数据库
database.append(user)
answer=input("是否继续注册用户?(y/n)")
if answer!='y':
break
print(database)