python 小试牛刀之信息管理

这个是之前写的半成品,但是一直没有好好的写完,今晚我把它补充完整,并且贴出了遇到的问题

这个程序并没有处理中文,主要是python 2.7对于中文的支持太蛋疼了,虽然可以设置utf8编码,但是如果列表中的某个变量是中文的话,打就会打印出那种我看不懂的编码,我干脆懒得理了,我想自己用python来处理中文的需求基本没有,可能以后就是用python谢谢程序玩玩而已

#!/usr/bin/python
# -*- coding: utf-8 -*-
from os import system
system("clear")

def menu():
	print "	   Welcome to the Information Management Center"
	print "			1.Add user"
	print "			2.View information"
	print "			3.Delete user"
	print "			4.Display all information"

def search():
	flag=0
	username=raw_input('Please enter the name that you wanna view: ')
	print info.get(username,'Sorry,i can not find it')
	raw_input('Press enter to continue')
def add():
	name=raw_input('NAME: ')
	for temp in info:
		if name==temp:
			raw_input('The name you enterd already exist!(Press enter to continue)')
			return
	birth=input('BIRTHDAY: ')
	job=raw_input('OCCUPATION: ')
	info[name]=(birth,job)
	print 'done'
	raw_input('Press enter to continue')
def delete():
	flag=0
	name=raw_input('Enter the name what you want to delete: ')
	#for temp in info:这样迭代并且删除有问题
	for temp in info.keys():
		if name==temp:
			flag=1
			del info[name]
			print 'done'
			raw_input('Press enter to continue')
	if flag==0:
		print 'I can not find the user.'
		raw_input('Press enter to continue')
def showall():
	flag=0
	for name in info:
		flag=1
		print name,info[name]
	if flag==0:
		print 'No data'
	raw_input('Press enter to continue')

info={}
info['tcstory']=(1993,"student")
while 1:
	menu()
	try:
		choice=input("Enter your choice(q to quit): ")
		if int(choice)!=choice and abs(choice)+choce==0:
			print 'Please enter the correct option
'
			system("clear")
			continue
	except (NameError,ValueError,SyntaxError): #syntaxerror 是为了防止一开始就直接输入回车键引起的错误
		print "OKey,goodbye!"
		break
	if choice==1:
		add()
		system('clear')
	elif choice==2:
		search()
		system('clear')
	elif choice==3:
		delete()
		system('clear')
	elif choice==4:
		showall()
		system('clear')
	else:
		print 'Your choice is invalid.'

之前遇到了一个问题,就是删除某个键的时候出错了

百度了一下,这个博客给出了解决的方法

 http://www.cnblogs.com/codeape/archive/2012/11/21/2780534.html

接下来我还要对这个程序进行修改,1.把所用到的函数放入一个模块中,2.从文件中读取数据和吧数据写入文件中

这个是明天的工作,今晚还要学习english和看python 的模块和文件操作部分,并且还要看一下vim 中syntaxtic 的插件的使用方法

原文地址:https://www.cnblogs.com/tcstory/p/3315736.html