#!/usr/bin/env python //程序开始
#File: information_service.py
#Date: 2016-1-9
INFO = { //定义INFO字典
'user1':{
'Phone': '010',
'Address': 'BJ',
'Work': 'IT'
},
'user2':{
'Phone': '021',
'Address': 'SH',
'Work': 'OP'
},
'user3':{
'Phone': '020',
'Address': 'GZ',
'Work': 'SL'
},
}
QUERY = { //定义QUERY字典
'Phone': 'call',
'Address': 'place',
'Work': 'do'
}
while True:
name = raw_input("Please input user you want for infomatin:") .strip() //.strip()表示可以有空格
while name in INFO:
print "Congratulation, %s in INFO list!" % name //%s表示一个字符串,% name引用变量
request = raw_input("Please input other infomation:Phone('P')/Address(A)/Work(W)") .strip()
if request == 'P' : key = 'Phone' //如果输入内容为'P',将其赋值给key
if request == 'A' : key = 'Address'
if request == 'W' : key = 'Work'
print "%s's %s is %s" % (name,QUERY[key],INFO[name][key]) //取字典的value值
break //查询成功,跳出整个循环
else:
choose = raw_input("Your input user ont in INFO, you can try again,input Y/N, try: ") .strip()
if choose == 'Y':
continue //如果要继续查询,重新循环
else:
break //程序结束
转载于:https://blog.51cto.com/strike/1733381