我的第一个python程序

本文介绍了一个使用Python编写的简单地址簿程序,该程序能够实现联系人的添加、删除、修改及查询等功能。通过此项目,作者进一步熟悉了Python的列表与字典操作,并计划后续学习Python界面编程。

人生苦短,我用python!

最近几天看了一下简明python教程,然后最后写了一个python查询、修改、删除、显示人名称和地址的小程序。菜鸟刚刚入门,啥也不会。

我为什么要学习python?我这个人就是有这个毛病,啥都想知道,但是啥都学不精通。唉,啥时候能踏踏实实好好的做好一件事情呢。这是我的第一个python程序,虽然菜,但还是贴出来做个纪念吧~这个程序让我对列表、字典都有了更进一步的了解。下一步就是看下python界面编程吧,然后学好了以后写一个简单的python贪吃蛇吧。这个学习也要继续进行下去。

好久没搞驱动了,还得再看看。师兄马上就毕业了,我得加紧跟师兄多沟通沟通。模型仿真控制这边也要弄。时不我待,再不学习就老了!

#!/usr/bin/python
#Filename:address.py

import sys

ab={}	#define a empty dict

class Person:
	num=0
#	name=[0]*50
#	address=[0]*50
	name=[]
	address=[]

	def add(self):
		print'add a name and  new address!'
		#Person.name[Person.num]=raw_input('please input the name:')
		#Person.address[Person.num]=raw_input('please input the address:')
		#ab[Person.name[Person.num]]=Person.address[Person.num]
		Person.name.append(raw_input('please input the name:'))
		Person.address.append(raw_input('please input the address:'))
		Person.num+=1
		#print'%s' % Person.name[Person.num-1]
		ab[Person.name[Person.num-1]]=Person.address[Person.num-1]

	def delete(self):
		if Person.num==0:
			print'there is no address!'
		else:
			print'delete a address!'
			delname=raw_input('please enter the name that you want to delete:')
			if delname in ab:
				var=raw_input('do you really want to  delete %s?(Y/N)' % delname)
				if var=='Y' or var=='y':
					del ab[delname]
					print'you have delete the %s' % delname
					Person.num-=1
				elif var=='N' or var=='n':
					print'you have not deleted the %s'% delname
				else:
					print'please input the right character!'
			else:
				print"what you input doesn't in the address book!"

	def modify(self):
		print'modify a address!'
		modname=raw_input('please enter the name that you want to modify the address:')
		if modname in ab:
			modaddress=raw_input('please input the new address:')
			ab[modname]=modaddress
		else:
			print'the name you enter does not in the address book!'


	def search(self):
		print'search the address!'
		value=raw_input("what do you want to search?N(name)or A(address):")
		if value=='N':
			seaname=raw_input('please input the name that you want to search:')
			if seaname in ab:
				print'the name is:%s,the address  is:%s' % (seaname,ab[seaname])
			else:
				print'the name you enter does not in the address book!'
		#elif value=='A':
		#	seaaddress=raw_input('please input the address that you want to search')
		#	if seaaddress in ab.items():
		#		print'the name is:%s,the address is:%s' % ()

	def show(self):
		print'show the address!'
		#print'the name list length is:%d' % len(Person.name)
		#print'the address list length is:%d' % len(Person.address)
		#for element in Person.name:
		#	print'the name is:%s' % element
		print'there are %d contacts in the address book' % len(ab)
		for name,address in ab.items():
			print'name is:%s,address is:%s' % (name,address)

token=True
p=Person()

print '1 for add the address'
print '2 for delete the address'
print '3 for modify the address'
print '4 for search the address'
print '5 for show the address'
print '0 for exit the program'
s=int(raw_input('Please input the operation you want to do:'))

while token:
	if s==1:
		p.add()
	elif s==2:
		p.delete()
	elif s==3:
		p.modify()
	elif s==4:
		p.search()
	elif s==5:
		p.show()
	elif s==0:
		sys,exit()
	s=int(raw_input('please input the operation:'))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值