Python:字典(Dictionary)基础应用

 字典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)

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值