python 新手,求解。。。。这是书上的一个例子,运行总是有问题,求各位大神帮忙看看!!!感激不尽。

2014-11-25 09:19:04 +08:00
 metas
#database.py
import sys,shelve
def store_person(db):
"""

Query user for data and store it in the shelf object
"""

pid = raw_input('Enter unique ID number:')
person = {}
person['name']=raw_input('Enter name:')
person['age']=raw_input('Enter age:')
person['phone']=raw_input('Enter phone:')

db[pid]=person
def lookup_person(db):
"""


Query user for ID and desired filed,and fecth the corresponding data form the shelf object
"""

pid = raw_input('Enter ID number')
filed = raw_input('What would you like to know?(name,age,phone)')
filed = filed.strip().lower()
print filed.capitalize() + ':',db[pid][filed]
def print_help():
print'The availabel command are:'
print'store: Store information about a person'
print'lookup: Lookup a person form a ID number'
print 'quit: Save changes and exit'
print'? : prints this message '
def enter_command():
cmd = raw_input('Enter command(? for help):')
cmd = cmd.strip().lower()
return cmd

def main():
database = shelve.open('C:\\database.dat ')
try:
while Ture:
cmd = enter_command()
if cmd =='store':
store_person(database)
elif cmd =='lookup':
lookup_person(database)
elif cmd =='?':
print_help
elif cmd =='quit':
return
finally:
database.close()

if __name__ == '__main__':
main()
3569 次点击
所在节点    问与答
20 条回复
metas
2014-11-25 09:20:02 +08:00
空格自动没了。。。没了。。
tankb52
2014-11-25 09:29:56 +08:00
不能贴代码吗?
tankb52
2014-11-25 09:31:19 +08:00
http://www.v2ex.com/t/74249?p=2

有贴代码的方法,楼主重新来一遍吧
lcqtdwj
2014-11-25 09:44:49 +08:00
贴报错啊。。。
libo26
2014-11-25 10:02:06 +08:00
把代码贴到gist上
https://gist.github.com/
letv
2014-11-25 10:13:45 +08:00
错误信息呢?
letv
2014-11-25 10:15:41 +08:00
while Ture:

改成while True:
huigeer
2014-11-25 10:28:23 +08:00
楼上真相帝
metas
2014-11-25 12:46:00 +08:00
@tankb52 不好意思啊,我打不开。。
metas
2014-11-25 12:46:38 +08:00
@letv 已改,还是报错
metas
2014-11-25 12:55:14 +08:00
metas
2014-11-25 12:57:00 +08:00
@tankb52 代码在下面
metas
2014-11-25 12:58:05 +08:00
@lcqtdwj 已贴,代码在下面,麻烦了
jsuper
2014-11-25 13:17:54 +08:00
@metas 没看到代码贴在哪儿了。。
metas
2014-11-25 13:30:16 +08:00
@jsuper 中间不是有一个空的吗。。。刚才我贴在那里了。。。。现在不见了
Sylv
2014-11-25 14:34:04 +08:00
你的代码我试了在 Mac 下运行正常,看报错应该是 database.dat 文件的问题,把 C:\\database.dat 删了再运行试下吧
metas
2014-11-25 15:43:57 +08:00
@Sylv 我在kali下运行了还是报错
letv
2014-11-25 17:24:57 +08:00
@metas 把 C:\\database.dat 删了,改成database.dat,顺便在你的源文件目录下新建一个database.dat试试
metas
2014-11-25 17:55:47 +08:00
@letv 你的意思是代码没有问题吗,就是那个 C:\\database.dat 有问题吗?
aaaa007cn
2014-11-25 19:29:57 +08:00
@metas 可以运行,没有大问题
@letv 的意思可能是你没注意到系统的权限问题
比如在 win7 下并且 c 盘是系统盘
不给 python 提权(以管理员权限运行)默认是没法往系统盘 c 盘根目录写文件的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/149046

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX