看了一些基础的 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. 《service》-“linux命令五分钟系列”之二

    本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...

  2. 给id赋值

    var div = document.getElementByTagName('div') div.id="mydiv";div.setAttribute("id&quo ...

  3. javascript为目标标签添加class样式

    利用jQuery $("#目标id").addClass("加的class名字");

  4. PHP常用代码段:

    1.PHP加密解密   function encryptDecrypt($key, $string, $decrypt){      if($decrypt){          $decrypted ...

  5. CodeChef FNCS

    题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...

  6. bzoj2127: happiness

    Description 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友 ...

  7. nginx+uwsgi+WSGI applications

    uwsgi一个专业的部署运用的工具,不仅能够部署Python运用,还能够部署其他运用比如Perl,Ruby等 uWSGI 安装: pip install uwsgi WSGI application( ...

  8. windows 下安装nodejs及其配置环境

    第一步:下载安装文件下载nodejs,官网:http://nodejs.org/download/,我这里下载的是node-v0.10.28-x86.msi,如下图: 第二步:安装nodejs下载完成 ...

  9. BZOJ 2572 高速公路

    Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站.Y901高速公路是一条由N-1段路以及N个收 ...

  10. Hotel

    poj3667:http://poj.org/problem?id=3667 题目大意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,group ...