python---使用get方法访问‘数据库’实体

使用get方法访问‘数据库’实体

#simple database

#people
people = {
	'Alice':{
		'phone':'2341',
		'addr':'Foo drive 23'
	},
	'Beth':{
		'phone':'9102',
		'addr':'Bar street 42'
	},
	'Ceil':{
		'phone':'3158',
		'addr':'Baz avenue 90'
	}
}

#describe
labels = {
	'phone':'phone number',
	'addr':'address'
}

name = raw_input('Name:')

request = raw_input('Phone number (p) or adress (a)?')

#find phone or address
if request == 'p':key = 'phone'
if request == 'a':key = 'address'

#use get() set default value
person = people.get(name,{})
label = labels.get(key,key)
result = person.get(key,'not avaliable')

print "%s's %s is %s." % (name,label,result)
#if name in people: print "%s's %s is %s." % (name,labels[key],people[name][key])

你可能感兴趣的:(python---使用get方法访问‘数据库’实体)