import pickle
import os
import time
class Person():
def __init__(self, number, address):
self.number = number
self.address = address
def save():
with open('mydict.pickle', 'wb') as f:
pickle.dump(mydict, f)
mydict = {}
mydict['Avi'] = ['347-000-0000', 'Oceanview']
mydict['Alan'] = ['347-000-0000', 'Brighton']
mydict['Frank'] = ['718-000-0000', 'Brighton']
print('add a name to the database.')
name = input('Name:')
number = input('Digits:')
address = input('Address:')
mydict[name] = [number, address]
-------------------------------------------------------
错误:
如果我尝试向数据库添加名称,则会收到名称错误。 NameError:名称“alan”未定义。奇怪的是字符串不起作用,但数字可以。抱歉,如果我的问题不清楚。
Traceback (most recent call last):
File "C:/Python33/ss", line 21, in <module>
name = input('Name:')
File "<string>", line 1, in <module>
NameError: name 'alan' is not defined
>>>
看来您正在使用 Python 2.x。
Use raw_input http://docs.python.org/2/library/functions.html#raw_string代替input http://docs.python.org/2/library/functions.html#input从用户那里获取字符串。
如果您正在阅读的书籍/材料假设读者使用的是 Python 3.x,那么最好使用 Python 3.x 而不是 Python 2.x。
顺便说一句,字典键区分大小写。
>>> d = {'Avi': 1, 'Alan': 2}
>>> d['alan']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'alan'
>>> d['Alan']
2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)