看了一些基础的 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/,转载请注明出处。

第一个 Python 程序 - Email Manager Demo的更多相关文章

  1. 第一个python程序

    一个python程序的两种执行方式: 1.第一种方式是通过python解释器: cmd->python->进入python解释器->编写python代码->回车. 2.第二种方 ...

  2. 3.第一个python程序

    学习任何一门语言的第一步,首先要写个'hello world',这算是程序员的一个传统.但在写之前,还有注意几个问题. 首先,python是一门脚本语言,而脚本语言的特点就是:我们写的代码会先由解释器 ...

  3. python入门(4)第一个python程序

    python入门(4)第一个python程序 在交互式环境的提示符>>>下,直接输入代码,按回车,就可以立刻得到代码执行结果.现在,试试输入100+200,看看计算结果是不是300: ...

  4. python笔记:#002#第一个python程序

    第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3​​.x 版本简介 执行 Python 程序的三种方式 解释器 -- python / python ...

  5. 2.第一个python 程序

    第一个python程序 一..python程序的编写步骤 1.创建  xxx.py文件(文件名不要中文) 文件名要以py为扩展名,因为导入的时候其他扩展名会报错.如果不导入的情况可以不限制扩展名. 2 ...

  6. python基础学习(一) 第一个python程序

    1. 使用python/python3解释器的方式 按照惯例,我们都是以Hello world作为一门程序语言的开始,进行如下的操作: 在桌面上新建一个hello-python文件夹 进入hello- ...

  7. 1.3 第一个python程序

    使用Pycharm编写第一个python程序 1.打开 Pycharm,选择 Create New Project,创建一个新项目 2.选择Pure Python表示创建一个纯Python程序项目,  ...

  8. 运行第一个Python程序

    Python的三种运行方式 交互式解释器 在终端输入python3 进入python交互式解释器 输入exit()退出交互式解释器 命令行脚本 创建python脚本 通过命令执行程序 python h ...

  9. 人生苦短之---第一个Python程序

    第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3​​.x 版本简介 执行 Python 程序的三种方式 解释器 —— python / python ...

随机推荐

  1. 【BZOJ2038】【莫队】小z的袜子

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  2. PHP中的&传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么?

    PHP中的&传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么? 代码如下: <?php $arr = array('one','two','three'); fo ...

  3. JQuery对单选框,复选框,下拉菜单的操作

    JSP <%@ page language="java" import="java.util.*" pageEncoding="utf-8&qu ...

  4. apache 设置404页面

    这几天用xampp搭建了一套环境,后来发现在网页访问出现404的时候xampp显示的内容不安全,把apache.php还有一些其它的版本都会显示 出来,所以想自己设置一个404的页面,在网上找了一些资 ...

  5. python自动开发之(ajax)第二十天

    1.Django请求的生命周期 路由系统 -> 试图函数(获取模板+数据=>渲染) -> 字符串返回给用户 2.路由系统 /index/ -> 函数或类.as_view() / ...

  6. 不定参数函数原理以及实现一个属于自己的printf函数

    一.不定参数函数原理 二.实现一个属于自己的printf函数 参考博文:王爽汇编语言综合研究-函数如何接收不定数量的参数

  7. X-Plosives

    uvaLive 3644:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pa ...

  8. IIC协议及其对ACK应答信号的处理

    1,SCL一直由Master控制,SDA依照数据传送的方向,读数据时由Slave控制SDA,写数据时由Master控制SDA.当8位数据传送完毕之后,应答位或者否应答位的SDA控制权与数据位传送时相反 ...

  9. 获取Delphi所有类的类信息

    Delphi遍历进程中所有Class的TypeInfo,即便是在implementation中的class或者其他 class的private的子class. 一般普通EXE中的TypeInfo存放在 ...

  10. MySql的卸载问题

    windows下mysql的卸载: 彻底卸载Mysql的方法:   (1),先在服务(开始——>控制面板——>管理工具——>服务)里停掉MySQL的服务.打开控制面板-添加删除程序, ...