#!/usr/bin/env pythoncoding:utf8Author:zhuimaEmail:993182876@qq.comDate:2015-03-23Function:Create the address book step by stepVersion:0.1
Initialized variables
msg = '''
Add information
Display information
Exit
'''
txl content like this tex = [['name','gender','telphone'],['name','gender','telphone']]
txl = []
define Add
define display
while True:
print msg
op = raw_input('Please Select >>> ')
if op == '1':
name = raw_input('Please Enter Your name >>> ')
gender = raw_input('Please Enter Your gender >>> ')
tel = raw_input('Please Enter Your Telphone Number >>> ')
txl.append([name,gender,tel])
elif op == '2':
for list in txl:
for info in list:
print info,
print ''
elif op == '0':
break
else:
print ''
print 'Unkonw Choose,Please Select again!'
print ''</pre><h2> <br /></h2><br><br /><span style="font-size:16px;"><strong>用到的python功能:</strong></span>
<ol class="list-paddingleft-2"><li><span style="font-size:16px;line-height:1.5;">while循环:实现循环输入</span></li><li><span style="font-size:16px;line-height:1.5;">if判断:判断输入内容并进行调用相关函数</span></li><li><span style="font-size:16px;line-height:1.5;">break:跳槽循环</span></li><li><span style="font-size:16px;line-height:1.5;">列表:在当前会话中保存数据</span></li><li><span style="font-size:16px;">三引号的使用</span></li></ol><strong><span style="font-size:16px;">测试结果:</span></strong><img src="https://s3.51cto.com/wyfs02/M02/5B/A7/wKioL1UP3yvgvP4xAAHMTL3rC7o475.jpg" border="0"><strong><span style="font-size:16px;">Note:</span></strong><span style="font-size:16px;"> print打印时,不换行使用’,’来实现,但是针对嵌套列表来说,很可能出现下面的情况,这个时候在跳出当前循环print空白行即可。</span><br /><h2>第二步:函数复用<br /><img src="https://s3.51cto.com/wyfs02/M02/5B/AC/wKiom1UP3grAG-BSAAGYqMPL5mQ502.jpg" border="0"> </h2><br> <span style="font-size:16px;">如此我们也能实现我们想要的功能,但是总感觉有点别扭,要不要来点高大上的,来来来,叫来函数来一发~</span>
<br /><span style="font-size:16px;">代码如下:</span>
<pre class="brush:xml;toolbar:false">#!/usr/bin/env python
coding:utf8
Author:zhuima
Email:993182876@qq.com
Date:2015-03-23
Function:Create the address book step by step
#Initialized variables
msg = '''
Add information
Display information
Exit
'''
txl content like this tex = [['name','gender','telphone'],['name','gender','telphone']]
txl = []
define Add
def Add():
name = raw_input('Please Enter Your name >>> ')
gender = raw_input('Please Enter Your gender >>> ')
tel = raw_input('Please Enter Your Telphone Number >>> ')
txl.append([name,gender,tel])
define display
def Disp():
for list in txl:
for info in list:
print info,
while True:
print msg
op = raw_input('Please Select >>> ')
if op == '1':
#Initialized variables
msg = '''
Add information
Display information
Delete by name
Update by name
Exit
'''
info = '''
name
gender
tel
all
'''
define change
def Change():
xingming = raw_input('Please Enter which one name you want to change >>> ')
print info
op = raw_input('Please select which one you want to change: ')
for sub in txl:
if sub[0] == xingming:
if op == '001':
name = raw_input('Please Enter Your name: ')
txl[txl.index(sub)][0] = name
elif op == '002':
gender= raw_input('Please Enter Your gender: ')
txl[txl.index(sub)][1] = gender
elif op == '003':
tel = raw_input('Please Enter Your tel: ')
txl[txl.index(sub)][2] = tel
else:
name = raw_input('Please Enter Your name: ')
gender= raw_input('Please Enter Your gender: ')
tel = raw_input('Please Enter Your tel: ')
txl[txl.index(sub)] = [name,gender,tel]
break
Load()
while True:
print msg
op = raw_input('Please Select >>> ')
if op == '1':
''' txl content like this tex = [['name','gender','telphone'],['name','gender','telphone']]
txl = []
define Add
def Add():
name = raw_input('Please Enter Your name >>> ')
gender = raw_input('Please Enter Your gender >>> ')
tel = raw_input('Please Enter Your Telphone Number >>> ')
txl.append([name,gender,tel])
define display
def Disp():
for list in txl:
for info in list:
print info,
print ''
define save
def Save():
temp = []
for info in txl:
temp.append(','.join(info))
s = '\n'.join(temp)
fp = file('txl.db','w')
fp.write(s+'\n')
fp.close()
define load
def Load():
import os
if os.path.exists('txl.db'):
fp = file('txl.db','r')
content = fp.read()
fp.close()
temp = content.split('\n')
for info in temp:
txl.append(info.split(','))
else:
fp = file('txl.db','w')
fp.close()
define del
def Del():
name = raw_input('Please Enter which one name you want to delete >>> ')
for sub in txl:
if sub[0] == name:
txl.remove(sub)
break
define change
def Change():
xingming = raw_input('Please Enter which one name you want to change >>> ')
print info
op = raw_input('Please select which one you want to change: ')
for sub in txl:
if sub[0] == xingming:
if op == '001':
name = raw_input('Please Enter Your name: ')
txl[txl.index(sub)][0] = name
elif op == '002':
gender= raw_input('Please Enter Your gender: ')
txl[txl.index(sub)][1] = gender
elif op == '003':
tel = raw_input('Please Enter Your tel: ')
txl[txl.index(sub)][2] = tel
else:
name = raw_input('Please Enter Your name: ')
gender= raw_input('Please Enter Your gender: ')
tel = raw_input('Please Enter Your tel: ')
txl[txl.index(sub)] = [name,gender,tel]
break
Load()
while True:
print msg
op = raw_input('Please Select >>> ')
if op == '1':
Add()
Save()
elif op == '2':
Disp()
elif op == '3':
Del()
Save()
elif op == '4':
Change()
Save()
elif op == '0':
break
else:
print ''
print 'Unkonw Choose,Please Select again!'