pytion学习1
个人感觉学习一门新语言,简单的语法懂一点足矣。接下来就是编程。读懂别人程序的每一句,理解每一句的意义。
#Filename:MyAddressBook.py
import cPickle as p
import os
#Class Item
class Item:
def __init__(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
#the main menu of address book
def menu():
'''the main menu of address book'''
print ''
print '1.Insert an item'
print '2.Delete an item'
print '3.Modify an item'
print '4.Display all items'
print '5.Sort all items'
print '6.Exit the program'
print 'What do you want to do?'
#initialization of system, load the member list
def begin():
'''initialization of system, load the member list'''
global itemlist
if os.path.exists('memberlist.data') == True:#to judge whether the file exists
listfile = file('memberlist.data','r')
if len(listfile.read())!=0:#to judge whether the file is empty
itemlist = p.load(listfile)
listfile.close()
#exitance of system, store the menber list
def end():
'''exitance of system, store the menber list'''
global itemlist
listfile = file('memberlist.data','w+')
p.dump(itemlist,listfile)
listfile.close()
#insert an item into the member list
def insert():
'''insert an item into the member list'''
name = raw_input('Enter name:')
age = int(raw_input('Enter age:'))
gender = raw_input('Enter gender:')
item = Item(name,age,gender)
global itemlist
itemlist.append(item)
#print an item
def output(item):
'''print an item'''
print '%-15s%-5d%s'%(item.name,item.age,item.gender)
#print all items
def display():
'''print all items'''
global itemlist
l = len(itemlist)
print 'name age gender'
for i in range(0,l):
output(itemlist[i])
print ''
#delete an item by name from member list
def delete():
'''delete an item by name from member list'''
name = raw_input('Enter the name you want to delete:')
global itemlist
l = len(itemlist)
for i in range(0,l):
if (itemlist[i].name == name):
itemlist.pop(i)
#update an item
def update(item):
'''update an item'''
item.name = raw_input('Enter name:')
item.age = int(raw_input('Enter age:'))
item.gender = raw_input('Enter gender:')
#update an item's information by name
def modify():
'''update an item's information by name'''
name = raw_input('Enter the name you want to modify:')
global itemlist
l = len(itemlist)
for i in range(0,l):
if (itemlist[i].name == name):
update(itemlist[i])
print 'Update done!'
#sort all items by name
def sort():
global itemlist
itemlist.sort(None,key = lambda item:item.name)
#Here are the scripts
itemlist = [] #Notice here!!!
begin()
while True:
menu()
sel = int(raw_input())
if sel == 1:
insert()
elif sel == 2:
delete()
elif sel == 3:
modify()
elif sel == 4:
display()
elif sel == 5:
sort()
else:
break
end()
print 'Good Bye!'
pytion学习1的更多相关文章
- 从直播编程到直播教育:LiveEdu.tv开启多元化的在线学习直播时代
2015年9月,一个叫Livecoding.tv的网站在互联网上引起了编程界的注意.缘于Pingwest品玩的一位编辑在上网时无意中发现了这个网站,并写了一篇文章<一个比直播睡觉更奇怪的网站:直 ...
- Angular2学习笔记(1)
Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- Unity3d学习 制作地形
这周学习了如何在unity中制作地形,就是在一个Terrain的对象上盖几座小山,在山底种几棵树,那就讲一下如何完成上述内容. 1.在新键得项目的游戏的Hierarchy目录中新键一个Terrain对 ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- 菜鸟Python学习笔记第一天:关于一些函数库的使用
2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...
- 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
随机推荐
- vbs常用代码
在网上查找资料的时候发现好多经典的vbs代码收集起来也为了以后学习. VBS播放音乐 Dim wmp Set wmp = CreateObject("WMPlayer.OCX") ...
- PKU 1002解题总结
闲来无事,研究了一下PKU1002的题目,大意就是把含有字母的电话号码,转换为数字,然后再计算每个电话号码出现的次数,输出.本来蛮简单的一道题,结果折腾了好久,主要也是自己的水平太菜了,先是直接用字符 ...
- 高通CP Crash分析调试
1. 转换tlcore文件 获取 EBICS0.BIN tl2elf --qconly tlcore 2.使用T32 命令把Riva的dump信息从EBICS0文件分离出来 data.load.BIN ...
- Python12期培训班-day1-登陆验证代码分享
#!/usr/bin/env python import sys import getpass afile = 'afile' bfile = 'bfile' circulation_num=0 #循 ...
- SVD
SVD分解(奇异值分解),本应是本科生就掌握的方法,然而却经常被忽视.实际上,SVD分解不但很直观,而且极其有用.SVD分解提供了一种方法将一个矩阵拆分成简单的,并且有意义的几块.它的几何解释可以看做 ...
- Extjs控制面板组件
(1)aoolyTo:(id) renderTo:(id)呈现在哪个html里面,同上 id最好用"" contentEI:() 呈现哪个html元素里面,把eI内的内容呈现 ( ...
- Mustache 使用心得总结
Mustache 使用心得总结 前言: 之前的一个项目里面就有用到这个前台的渲染模版,当时挺忙的也没时间抽空总结一下,刚好上周项目里又用到这个轻量型的渲染模版,真心感觉很好用,因此就总结一下使用心得, ...
- PHP模版引擎 – Twig
在网站开发过程中模版引擎是必不可少的,PHP中用的最多的当属Smarty了.目前公司系统也是用的Smarty,如果要新增一个页面只需把网站的头.尾和左侧公共部分通过Smarty的include方式引入 ...
- 【freemaker】之自定义指令<#macro>
测试代码 @Test public void test07(){ try { root.put("name", "张三"); freemakerUtil.fpr ...
- IOS中图片拉伸技巧与方法总结(转载)
以下内容转载自:http://my.oschina.net/u/2340880/blog/403996 IOS中图片拉伸技巧与方法总结 一.了解几个图像拉伸的函数和方法 1.直接拉伸法 简单暴力,却是 ...