python初体验
一个同事要离职了,她把python相关的东西交给了我。祝她好运
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 23 16:22:05 2011
一个通讯录程序
一个同事要离职了,她把python相关的东西交给了我。祝她好运
就这么个小东西,居然写了快两个点儿,留个纪念。
@author: Joey
"""
import os
class Info:
def __init__(self,name,address,phone):
self.name = name
self.address = address
self.phone = phone
class InfoAccess:
def save(self,info):
f = file('address.txt', 'a') # open for 'a'ppend
f.write(info.name.join('\r\n'))
f.write(info.address.join('\r\n'))
f.write(info.phone.join('\r\n'))
f.close() # close the file
def getAll(self):
list = []
f = file('address.txt')
while True:
line = f.readline()
if len(line) == 0:
break
else:
name = line;
address = f.readline();
phone = f.readline();
info = Info(name,address,phone)
list.append(info)
return list
infoAccess = InfoAccess()
while(1):
os.system("cls")
print('--------------------address---------------------')
print(' a: new address')
print(' b: view address list')
print(' c: exit')
inputStr = (raw_input())
if inputStr == 'a':
print('input name:')
name = raw_input()
print('input address:')
address = raw_input()
print('intput phone:')
phone = raw_input()
info = Info(name, address, phone)
infoAccess.save(info)
elif inputStr == 'b':
list = infoAccess.getAll()
for element in list:
print('------------------------')
print('name:')
print(element.name)
print('address')
print(element.address)
print('phone:')
print(element.phone)
print("press anykey to continue")
raw_input()
elif inputStr == 'c':
break
else:
print('error')
欢迎转载,转载请注明出处