字典Dictionary 键值对的形式,一个key对应一个value,一个dictionary中,key是唯一的
get()可以在访问到不存在的量时,可以人为设置值
而且 get()即使访问不到,程序也会正常运行
但是print(customer["birthday"]) 会直接结束程序
customer = {
"name": "Stella",
"age": 18,
"is_verified": True
}
# 修改
customer["name"] = "jack "
# 增加
customer["telephone"] = 123456
print(customer["telephone"])
# 访问方式
print(customer["name"])
print(customer.get("name"))
# print(customer["birthday"])
print(customer.get("birthday"))
print(customer.get("birthday", "Gracia 1 19999"))
test:提示输入Phone (输入一串数字),输出英文形式
例如 Phone 1234
输出 one two three four
tips:"1"->"one"
答案:
telephone = {
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five"
}
output = ""
number = input("Phone ")
for x in number:
# output+=telephone[x]+" "
# 用get()保证当没有这个值时不会终止程序,且有标识
output += telephone.get(x, "!") + " "
print(output)