早教吧作业答案频道 -->英语-->
简明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后面的作业电...的网友还看了以下:
已知字母组合成英语单词1、e e t t i n h r 2、e e r a t w h 3、o 2020-05-14 …
英语单词辨音找出画()部分读音与其余不同的单词()1.A .kn(ee) B.n(e)ck C.r 2020-05-17 …
A、B两小球用长为L的细线连接悬挂在空中'.A距离湖水水面高度为H,释放小球'让他们自由下落'测得 2020-06-05 …
有一根长为L的钢管,当在一端打击一下时,某人在它的另一端听到两次响声,时间间隔为T,若声音在空气中 2020-06-06 …
重新排列字母,写出单词1.s,a,p,e,c,2.r,o,e,t,c,k,3.d,c,o,o,t, 2020-06-06 …
对英语单词熟练的人来!c,o,l,t,e,s()g,g,p,a,n,e,t,l,()gosomew 2020-06-07 …
c语言,t为int类型,进入下面的循环之前t的值为0,以下叙述正确的是t为int类型,进入下面的循 2020-06-15 …
设命题p:|2x-3|<1;命题q:lg2x-(2t+l)lgx+t(t+l)≤0,(1)若命题q 2020-08-03 …
三角形分解,矩阵设A是n阶非奇异方阵,试证:存在对角元是1的下三角形矩阵L和上三角形矩阵T使得A=L 2020-12-07 …
电路中,N为无源线性网络,电路中,N为无源线性网络,u(t)=10根号2cos(t+75度)V,l( 2020-12-18 …