定义变量:
name_for_userid = {
382: "Alice",
590: "Bob",
951: "Dilbert",
}
创建函数使用`.get` 方法:
def greeting(userid):
return "Hi %s!" % name_for_userid.get(userid, "there")
示例:
greeting(382)
"Hi Alice!"
greeting(333333)
"Hi there!"
name_for_userid = {
382: "Alice",
590: "Bob",
951: "Dilbert",
}
def greeting(userid):
return "Hi %s!" % name_for_userid.get(userid, "there")
greeting(382)
"Hi Alice!"
greeting(333333)
"Hi there!"
评论区