急求解答解决办法
急求解答
#Filename:address_book.py
import cPickle as p
class NoteBook:
def __init__(self):
friendinformation = { 'xsajs' : '12345678' "beijingshi",
'sdsd1' : '15233263' "anhuishenganqingshi",
'maasos' : '15443235' "suzhoushi",
'mdsdd1' : '134212112' "shanghaishi"
}
f=file('notebook.txt','w')
p.dump(friendinformation,f)
f.close()
def add_info(self,name,tel,address):
friendinformation[name]=tel+address
f=file('notebook.txt','w')
p.dump(friendinformation,f)
#f.write(add)
f.close()
f=file('notebook.txt')
fsbefore=p.load(f)
print fsbefore
def delete(self,name):
del friendinformation[name]
f=file('notebook.txt','w')
p.dump(friendinformation,f)
f.close()
f=file('notebook.txt','r')
fsafter=p.load(f)
f.close()
print fsafter
def inquiry(self,name):
print friendinformation[name]
Mynote=NoteBook()
while True:
manu = raw_input('''
1.添加信息;
2.删除信息;
3.查询信息;
4.退出
-->''')
if manu=='a':
name=raw_input('please input the name you want to add:')
tel=raw_input('please input the tel number you want to add:')
address=raw_input('please input the address you want to add:')
Mynote.add_info(name,tel,address)
elif manu=='d':
name=raw_input('please input the name you want to delete:')
Mynote.delete(name)
elif manu=='c':
name=raw_input('who is you want to inquary:')
Mynote.inquiry(name)
elif manu=='q':
sys.exit()
else:
print "don't have this option,please try input again."
运行结果:
1.添加信息;
2.删除信息;
3.查询信息;
4.退出
-->c
who is you want to inquary:
maasos
Traceback (most recent call last):
File "A:/Python/python2.7/address_book.py", line 55, in <module>
name=raw_input('who is you want to inquary:')
File "A:/Python/python2.7/address_book.py", line 36, in inquiry
print friendinformation[name]
NameError: global name 'friendinformation' is not defined
------解决思路----------------------
friendinformation 是在__init__中定义的啊,不是全局变量
------解决思路----------------------
加一句
或者全局定义一下
#Filename:address_book.py
import cPickle as p
class NoteBook:
def __init__(self):
friendinformation = { 'xsajs' : '12345678' "beijingshi",
'sdsd1' : '15233263' "anhuishenganqingshi",
'maasos' : '15443235' "suzhoushi",
'mdsdd1' : '134212112' "shanghaishi"
}
f=file('notebook.txt','w')
p.dump(friendinformation,f)
f.close()
def add_info(self,name,tel,address):
friendinformation[name]=tel+address
f=file('notebook.txt','w')
p.dump(friendinformation,f)
#f.write(add)
f.close()
f=file('notebook.txt')
fsbefore=p.load(f)
print fsbefore
def delete(self,name):
del friendinformation[name]
f=file('notebook.txt','w')
p.dump(friendinformation,f)
f.close()
f=file('notebook.txt','r')
fsafter=p.load(f)
f.close()
print fsafter
def inquiry(self,name):
print friendinformation[name]
Mynote=NoteBook()
while True:
manu = raw_input('''
1.添加信息;
2.删除信息;
3.查询信息;
4.退出
-->''')
if manu=='a':
name=raw_input('please input the name you want to add:')
tel=raw_input('please input the tel number you want to add:')
address=raw_input('please input the address you want to add:')
Mynote.add_info(name,tel,address)
elif manu=='d':
name=raw_input('please input the name you want to delete:')
Mynote.delete(name)
elif manu=='c':
name=raw_input('who is you want to inquary:')
Mynote.inquiry(name)
elif manu=='q':
sys.exit()
else:
print "don't have this option,please try input again."
运行结果:
1.添加信息;
2.删除信息;
3.查询信息;
4.退出
-->c
who is you want to inquary:
maasos
Traceback (most recent call last):
File "A:/Python/python2.7/address_book.py", line 55, in <module>
name=raw_input('who is you want to inquary:')
File "A:/Python/python2.7/address_book.py", line 36, in inquiry
print friendinformation[name]
NameError: global name 'friendinformation' is not defined
------解决思路----------------------
friendinformation 是在__init__中定义的啊,不是全局变量
------解决思路----------------------
加一句
global friendinformation
或者全局定义一下