看了一些基础的 Python 新手教程后,深深感觉到 Python 的简洁与强大,这是我的第一个 Python Demo。下面是完整代码与执行截图。

代码:

# encoding: utf-8
'''
@author: Techzero
@email: techzero@163.com
@time: 2014-4-30 下午1:31:04
'''
import os
import sys
import cPickle as p class Person:
def __init__(self, name, email):
'''Initializes the person's data.'''
self.name = name
self.email = email def create():
"""Create new person and input email"""
global Persons
try:
name = raw_input("Please input name:")
while Persons.has_key(name):
name = raw_input("This name has already exist, please input new name:")
email = raw_input("Please input Email:")
except EOFError:
print '\nEOF Error'
sys.exit()
Persons[name] = email
print "" def delete():
"""Search person by name and delete"""
global Persons
try:
name = raw_input("Please input the person's name you want to delete:")
except EOFError:
print '\nEOF Error'
sys.exit()
if Persons.has_key(name):
del Persons[name]
save()
else:
print "No one called",name,"!\n" def modify():
"""Search person by name and modify email"""
global Persons
try:
name = raw_input("Please input the person's name you want to modify:")
if Persons.has_key(name):
del Persons[name]
email = raw_input("Please input new email:")
Persons[name] = email
save()
else:
print "No one called",name,"!\n"
except EOFError:
print '\nEOF Error'
sys.exit() def save():
"""Save Persons to file"""
global Persons
File = 'person.dat'
f = file(File, 'w')
p.dump(Persons, f)
f.close()
print "Operation Done!\n" def read():
"""Read person from file"""
global Persons
File = 'person.dat'
if os.path.exists(File):
f = file(File)
Persons = p.load(f)
f.close()
else:
File = 'person.dat'
f = file(File, 'w')
f.close() def display():
"""Display all persons in the dictionary"""
global Persons
for name, email in Persons.items():
print " ",name,email
print "" def search():
"""Search person by name"""
global Persons
try:
name = raw_input("Please input the person's name you want to search:")
except EOFError:
print '\nEOF Error'
sys.exit()
if Persons.has_key(name):
print " ",name,Persons[name],"\n"
else:
print "No one called",name,"!\n" def menu():
"""Display a menu to choose operation"""
choose = "0"
while True:
#i = os.system("cls")
print'''1----Create
2----Delete
3----Modify
4----Search
5----Display
6----Exit'''
try:
choose = raw_input("Please choose an item(1-6):")
except EOFError:
print '\nEOF Error'
sys.exit()
if choose == "1":
create()
elif choose == "2":
delete()
elif choose == "3":
modify()
elif choose == "4":
search()
elif choose == "5":
display()
elif choose == "6":
print "Thanks for using!"
sys.exit()
else:
print "" Persons = {}
read()
menu()

执行截图



本文固定链接:http://www.itechzero.com/coding/python/python-development-with-eclipse-pydev-install-tutorial/,转载请注明出处。

最新文章

  1. 张小龙宣布微信小程序1月9日发布,并回答了大家最关心的8个问题
  2. xml Schema 基础
  3. 在Ubuntu上安装有道词典
  4. nodejs学习笔记二:解析express框架项目文件
  5. mysql前缀索引(字符串截取部分作为索引), 以及索引选择指引
  6. common.css
  7. EL表达式结合页面JSTL使用 迭代显示表格
  8. Longest Palindromic Substring 解答
  9. MSDTC问题集
  10. 最受欢迎的8位Java大师
  11. MongoDB_GridFS_存储文件
  12. android学习3——长宽的单位问题dp,px,dpi
  13. api-gateway实践(07)新服务网关 - 手动发布
  14. Js获取客户端用户Ip地址
  15. 用Sqoop进行Hive和MySQL之间的数据互导
  16. One point compactification
  17. ELK之安装了search guard认证后安装elasticsearch-head
  18. hive视图
  19. _CSS Hack
  20. Java Selenium - 几种对话框处理Alert\confirm\prompt

热门文章

  1. 『重构--改善既有代码的设计』读书笔记----Introduce Foreign Method
  2. JQuery select控件的相关操作
  3. iOS开发工程师笔试题
  4. 在 iOS 应用中直接跳转到 AppStore 的方法--备用
  5. beego路由实现原理
  6. 谈谈Parser --王垠
  7. Spring MVC控制器用@ResponseBody声明返回json数据报406的问题
  8. org.springframework.web.context.ContextLoaderListener
  9. 揭开Linux操作系统的Swap交换区之谜
  10. 自己动手实现Queue