标签:实战 函数 16 choice person persons print input name
# 创建一个全局列表容器来存储联系人字典
persons = []
# 创建联系人
def create_person():
name = input('name: ')
address = input('address: ')
phone = input('phone: ')
person = {'name': name, 'address': address, 'phone': phone}
persons.append(person)
# 列出所有联系人
def list_person():
for person in persons:
print('%s,%s,%s' % (person['name'], person['address'], person['phone']))
# 查询联系人
def query_person():
name = input('name: ')
for person in persons:
if person['name'] == name:
print('%s,%s,%s' % (person['name'], person['address'], person['phone']))
# 删除联系人
def delete_person():
name = input('name: ')
for person in persons:
if person['name'] == name:
persons.remove(person)
break
# 程序菜单
def get_choice():
print('1. create person')
print('2. list all persons')
print('3. query person')
print('4. delete person')
print('5. quit')
choice = input('Enter a number(1-5):')
return choice
# 主函数
def main():
while True:
choice = get_choice()
if choice == '1':
create_person()
elif choice == '2':
list_person()
elif choice == '3':
query_person()
elif choice == '4':
delete_person()
elif choice == '5':
break
else:
print('Invalid choice')
# 运行主程序
main()
参考资料
http://www.imooc.com/wiki/pythonlesson1/function2.html
标签:实战,函数,16,choice,person,persons,print,input,name 来源: https://www.cnblogs.com/tiansz/p/16389768.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。