第一个 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 ...
随机推荐
- SGU 226.Colored graph(最短路)
时间限制:0.25s 空间限制:4M 题意: 给出一个n个节点,m条边的图,每条边都有标记了编号为1,2,3三种颜色之一,现在求从1号节点到n号节点的一条最短路径的长度,要求该路径中相邻的边没有相同的 ...
- Winform控件Enable=false显示优化
在B/S开发中(ASP.NET),往往可以css样式表来让页面控件更加美观,但是在C/S中(Winform)里面,我们则需要通过其他取巧的 方式来实现.例如:当你因为某个需求需要将控件设置为Reado ...
- css杂项,清除浮动
在写HTML代码的时候,发现在Firefox等符合W3C标准的浏览器中,如果有一个DIV作为外部容器,内部的DIV如果设置了float样式,则外部的容器DIV因为内部没有clear,导致不能被撑开.看 ...
- function field , store={}...
def _get_product_state(self,cr,uid,ids,fields,arg=None,context=None): res={} dic=dict( self.pool.get ...
- lsmod
http://blog.csdn.net/yuan892173701/article/details/8960607 抽空写下
- python中对文件、文件夹的操作需要涉及到os模块和shutil模块。
创建文件:1) os.mknod("test.txt") 创建空文件2) open("test.txt",w) 直接打开一个文件,如果文件不存在则创建文件 创建 ...
- 弄懂css中单位px和em,rem的区别
国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 1. IE无法调整那些使用px作为单位的字体大小 ...
- NET笔试题集
题目来源于传智播客和各大互联网,复习.重新整理贴出来. 1.简述 private. protected. public. internal.protected internal 访问修饰符和访问权限 ...
- IIS短文件名漏洞修补方法之一改注册表一个注意项
1)1.png 为漏洞存在没有做任何修复的时候的扫描 修复:2) 修改注册表键值: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSy ...
- 架设wordpress再vps上的 一些感想总结
日本vps.樱花系列 配置: 2cpu+1G内存+100G硬盘 系统 第一次我把默认的centos 给换了..原因就是,不会linux.而且我主要用.net 感觉 mono也行.但是linux不会. ...