作业

1. 请用代码验证"name"是否在字典的键中
info = {"name": "王刚", "hobby": "铁锤", "age": 18}

# print("name" in info)
# print("name" in info.keys())                 # True
View Code
2. 循环提示用户输入,并将输入内容追加到列表中,如果输入N或者n则停止循环
例如: ['sdavav', 'nhft', 'lk', 'wewe']
l1 = []
while 1:
    content = input(">>>")
    if content.upper() == "N":
        break
    l1.append(content)
print(l1)
View Code
3. 循环提示用户输入,并将输入内容追加到字典中,如果输入N或者n则停止循环
例如: [{"name": "太白", "password": 123}, {"name": "sff", "password": 236}, {"name": "rg", "password": 568},...]
li = []
# dic = dict()
while 1:
    user_name = input("请输入用户名:")
    if user_name.upper() == "N":
        break

    password = input("请输入密码:")
    dic = dict()
    dic["name"] = user_name
    dic["password"] = password
    li.append(dic)
print(li)
View Code
4. 看代码写结果并解释原因
# v1 = {"k1": "v1", "k2": [1, 2, 3]}
# v2 = {"k1": "v1", "k2": [1, 2, 3]}
# result1 = v1 == v2
# result2 = v1 is v2
# print(result1)               # True
# print(result2)               # False
View Code
5. 看代码写结果并解释原因
变量指向的是真实的数据
# v1 = "人生苦短,用python"
# v2 = [1, 2, 3, v1]
#
# v1 = "人生苦短,用毛线python"
# print(v2)                            # [1, 2, 3, '人生苦短,用python']
# 理解成变量的赋值 例如 a = 10, b = a, c = b, b = 15

# info = [1, 2, 3]
# userinfo = {"account": info, "num": info }
# info.append(9)
# print(userinfo)                         # {'account': [1, 2, 3, 9], 'num': [1, 2, 3, 9]}

# info = [1, 2, 3]
# userinfo = {"account": info, "num": info }
# info = "好多题"
# print(userinfo)                         # {'account': [1, 2, 3], 'num': [1, 2, 3]}


# info = [1, 2, 3]
# userinfo = [info, info ,info , info]
# info[0] = "好嗨哟"
# print(info, userinfo)                 #  ['好嗨哟', 2, 3]   [['好嗨哟', 2, 3], ['好嗨哟', 2, 3], ['好嗨哟', 2, 3], ['好嗨哟', 2, 3]]

# info = [1, 2, 3]
# userinfo = [info, info ,info , info]
# userinfo[2][0] = "shut up"
# print(info, userinfo)                   #  ['shut up', 2, 3] [['shut up', 2, 3], ['shut up', 2, 3], ['shut up', 2, 3], ['shut up', 2, 3]]

# data_list = []
# data = {}
# for i  in range(5):
#     data["user"] = i
#     data_list.append(data)
# print(data)                      # {'user': 4}
# print(data_list)                 # [{'user': 4}, {'user': 4}, {'user': 4}, {'user': 4}, {'user': 4}]
View Code

 

posted @ 2020-07-29 11:02  Ethan272  阅读(204)  评论(0编辑  收藏  举报