第一个 Python 程序 - Email Manager Demo
看了一些基础的 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的更多相关文章
- 第一个python程序
一个python程序的两种执行方式: 1.第一种方式是通过python解释器: cmd->python->进入python解释器->编写python代码->回车. 2.第二种方 ...
- 3.第一个python程序
学习任何一门语言的第一步,首先要写个'hello world',这算是程序员的一个传统.但在写之前,还有注意几个问题. 首先,python是一门脚本语言,而脚本语言的特点就是:我们写的代码会先由解释器 ...
- python入门(4)第一个python程序
python入门(4)第一个python程序 在交互式环境的提示符>>>下,直接输入代码,按回车,就可以立刻得到代码执行结果.现在,试试输入100+200,看看计算结果是不是300: ...
- python笔记:#002#第一个python程序
第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3.x 版本简介 执行 Python 程序的三种方式 解释器 -- python / python ...
- 2.第一个python 程序
第一个python程序 一..python程序的编写步骤 1.创建 xxx.py文件(文件名不要中文) 文件名要以py为扩展名,因为导入的时候其他扩展名会报错.如果不导入的情况可以不限制扩展名. 2 ...
- python基础学习(一) 第一个python程序
1. 使用python/python3解释器的方式 按照惯例,我们都是以Hello world作为一门程序语言的开始,进行如下的操作: 在桌面上新建一个hello-python文件夹 进入hello- ...
- 1.3 第一个python程序
使用Pycharm编写第一个python程序 1.打开 Pycharm,选择 Create New Project,创建一个新项目 2.选择Pure Python表示创建一个纯Python程序项目, ...
- 运行第一个Python程序
Python的三种运行方式 交互式解释器 在终端输入python3 进入python交互式解释器 输入exit()退出交互式解释器 命令行脚本 创建python脚本 通过命令执行程序 python h ...
- 人生苦短之---第一个Python程序
第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3.x 版本简介 执行 Python 程序的三种方式 解释器 —— python / python ...
随机推荐
- 【转载】【树状数组区间第K大/小】
原帖:http://www.cnblogs.com/zgmf_x20a/archive/2008/11/15/1334109.html 回顾树状数组的定义,注意到有如下两条性质: 一,c[ans]=s ...
- 24种设计模式--代理模式【Proxy Pattern】
什么是代理模式呢?我很忙,忙的没空理你,那你要找我呢就先找我的代理人吧,那代理人总要知道被代理人能做哪些事情不能做哪些事情吧,那就是两个人具备同一个接口,代理人虽然不能干活,但是被代理的人能干活呀. ...
- C#快速导入海量XML数据至SQL Server数据库
#region 将Xml中的数据读到Dataset中,然后用SqlBulkCopy类把数据copy到目的表中using (XmlTextReader xmlReader = new XmlTextRe ...
- C# Windows服务安装出现System.Security.SecurityException异常解决办法
我把注册windows服务所用的安装及启用服务命令写到了bat可执行文件(名称为install.bat)中,如下所示: %SystemRoot%\Microsoft.NET\Framework\v4. ...
- 批处理文件安装与卸载Windows服务
//安装Windows服务 将RECPost.exe和RECPostService替换成自己的项目名称和服务名称,并将文件保存成bat格式.其中%cd%是获取相对路径 @echo off set fi ...
- WPF之核心面板(容器)控件简单介绍
一.Canvas 1.官方表述:定义一个区域,在该区域中可以使用相对于该区域的坐标显式定位子元素. 2.对于canvas 的元素的位置,是靠控件的大小及Canvas.Top.Canvas.Left.C ...
- 浅谈Chrome V8引擎中的垃圾回收机制
垃圾回收器 JavaScript的垃圾回收器 JavaScript使用垃圾回收机制来自动管理内存.垃圾回收是一把双刃剑,其好处是可以大幅简化程序的内存管理代码,降低程序员的负担,减少因 长时间运转而带 ...
- ubuntu 安装 open in teminal
sudo apt-get install nautilus-open-terminalnautilus -q
- document模板
http://bce.baidu.com/doc/CDS/GettingStarted.html
- 代码之美——Doom3源代码赏析2
http://www.csdn.net/article/2013-01-17/2813778-the-beauty-of-doom3-source-code/2 摘要:Dyad作者.资深C++工程师S ...