早教吧作业答案频道 -->英语-->
简明python后面的作业电话簿求问importpickleasplinkmanlistfile='Linkmanlist.data'classperson:definit(self,city,phonenumber):self.city=cityself.phonenumber=phonenumberdefsetcity(self,city):self.city=citydefsetphonenum
题目详情
简明python后面的作业电话簿求问
import pickle as p
linkmanlistfile = 'Linkmanlist.data'
class person:
def __init__(self,city,phonenumber):
self.city = city
self.phonenumber = phonenumber
def setcity(self,city):
self.city = city
def setphonenumber(self,phonenumber):
self.phonenumber = phonenumber
def getcity(self):
return self.city
def getphonenumber(self):
return self.phonenumber
try :
f = open(linkmanlistfile,'r')
linkmanlist = p.load(f)
except :
f = open(linkmanlistfile,'w')
linkmanlist = {}
p.dump(linkmanlist,f)
f.close()
while True:
print('Please Enter Your Choice:')
print('1.Add a linkman' )
print('2.Edit a linkman' )
print('3.Delete a linkman')
print('4.Look for a linkman')
print('5.Print the whole linkman list')
print('6.Exit')
choice=input('Now make your choice:')
print(' ')
if choice == '1':
thename = raw_input("Enter your new linkman's name:")
thename = thename.replace(' ','_')
if thename in linkmanlist.iterkeys():
print('Already include this people!')
else :
thecity = raw_input("Enter your new linkman's livecity:")
thecity = thecity.replace(' ','_')
thenum = raw_input("Enter your new linkman's phonenumber:")
f = file(linkmanlistfile,'r')
linkmanlist = p.load(f)
linkmanlist[thename] = person (thecity,thenum)
f = file(linkmanlistfile,'w')
p.dump(linkmanlist,f)
f.close()
print(' ')
if choice=='2':
thename = raw_input("Enter the one you want to edit linkman's name:")
thename = thename.replace(' ','_')
if thename in linkmanlist.iterkeys():
f = file(linkmanlistfile,'r')
linkmanlist = p.load(f)
n = 1
while n == 1:
print('Please choice what you want edit:')
print('1.Name')
print('2.City')
print('3.Phonenumber')
print('4.Exit')
对了我用的是python3.3
报错
Traceback (most recent call last):
File "E:/python/score.py",line 17,in
linkmanlist = p.load(f)
TypeError:'str' does not support the buffer interface
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "E:/python/score.py",line 21,in
p.dump(linkmanlist,f)
TypeError:must be str,not bytes
import pickle as p
linkmanlistfile = 'Linkmanlist.data'
class person:
def __init__(self,city,phonenumber):
self.city = city
self.phonenumber = phonenumber
def setcity(self,city):
self.city = city
def setphonenumber(self,phonenumber):
self.phonenumber = phonenumber
def getcity(self):
return self.city
def getphonenumber(self):
return self.phonenumber
try :
f = open(linkmanlistfile,'r')
linkmanlist = p.load(f)
except :
f = open(linkmanlistfile,'w')
linkmanlist = {}
p.dump(linkmanlist,f)
f.close()
while True:
print('Please Enter Your Choice:')
print('1.Add a linkman' )
print('2.Edit a linkman' )
print('3.Delete a linkman')
print('4.Look for a linkman')
print('5.Print the whole linkman list')
print('6.Exit')
choice=input('Now make your choice:')
print(' ')
if choice == '1':
thename = raw_input("Enter your new linkman's name:")
thename = thename.replace(' ','_')
if thename in linkmanlist.iterkeys():
print('Already include this people!')
else :
thecity = raw_input("Enter your new linkman's livecity:")
thecity = thecity.replace(' ','_')
thenum = raw_input("Enter your new linkman's phonenumber:")
f = file(linkmanlistfile,'r')
linkmanlist = p.load(f)
linkmanlist[thename] = person (thecity,thenum)
f = file(linkmanlistfile,'w')
p.dump(linkmanlist,f)
f.close()
print(' ')
if choice=='2':
thename = raw_input("Enter the one you want to edit linkman's name:")
thename = thename.replace(' ','_')
if thename in linkmanlist.iterkeys():
f = file(linkmanlistfile,'r')
linkmanlist = p.load(f)
n = 1
while n == 1:
print('Please choice what you want edit:')
print('1.Name')
print('2.City')
print('3.Phonenumber')
print('4.Exit')
对了我用的是python3.3
报错
Traceback (most recent call last):
File "E:/python/score.py",line 17,in
linkmanlist = p.load(f)
TypeError:'str' does not support the buffer interface
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "E:/python/score.py",line 21,in
p.dump(linkmanlist,f)
TypeError:must be str,not bytes
▼优质解答
答案和解析
注意看文档,文档中文明写了
”an on-disk file opened for binary reading“
也就是说你用open打开文件的时候,读需要用“rb”,写需要用“wb”
”an on-disk file opened for binary reading“
也就是说你用open打开文件的时候,读需要用“rb”,写需要用“wb”
看了 简明python后面的作业电...的网友还看了以下:
不等式命题等价的问题t∈R,关于m的不等式m>(t^2-1)/(t-2)的否命题是m≤(t^2-1) 2020-03-30 …
关于x的分式方程x-x分之一=t-t分之一的解为x1=t,x2=﹣t分之一:x+x分之一=t+t分 2020-05-01 …
用四阶龙格库塔法求解矩阵微分方程要求电流就是求解矩阵微分方程:(R+pM(t))*I(t)+M(t 2020-05-13 …
设函数f(x)=tx²+2t²x+t-1(t∈R,t>0)(1)求f(x)的最小值h(t)(2)若 2020-06-02 …
讨论:关于如何求卷积x(t)*h(-t)的积分表达式?以前信号与系统里学过了x(t)*h(t)的表 2020-06-06 …
如图,质量分别为m=1kg和M=2kg的两物块叠放在光滑水平桌面上,两物块均处于静止状态,从某时刻 2020-07-07 …
(m(t)f(t))*g(t)能不能等于m(t)(f(t)*g(t))?其中*是卷积符号 2020-07-09 …
在平面直角坐标系xoy中,以原点O为极点,以x轴正半轴为极轴建立极坐标系.已知直线l的参数方程为x 2020-07-31 …
高斯过程求期望如果X1(t),X2(t),X3(t),X4(t)都是高斯随机过程那么E[X1(t)X 2020-10-31 …
已知函数f(x)=x^3-x(1)求曲线y=f(x)在M(t,f(t))处(2)设a>0,如果过点P 2020-11-03 …