V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
metas
V2EX  ›  问与答

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

  •  1
     
  •   metas · 2014-11-25 09:19:04 +08:00 · 3553 次点击
    这是一个创建于 3456 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #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()
    第 1 条附言  ·  2014-11-25 12:45:35 +08:00
    true已改

    下面是错误信息
    Traceback (most recent call last):
    File "database.py", line 55, in <module>
    main()
    File "database.py", line 39, in main
    database = shelve.open('C:\\database.dat ')
    File "C:\Python27\lib\shelve.py", line 239, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
    File "C:\Python27\lib\shelve.py", line 223, in __init__
    Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
    File "C:\Python27\lib\anydbm.py", line 82, in open
    raise error, "db type could not be determined"
    anydbm.error: db type could not be determined

    https://gist.github.com/这个我打不开啊,可能这几天 电信抽风了
    第 2 条附言  ·  2014-11-25 12:55:57 +08:00
    代码在下面,麻烦大家了。
    第 3 条附言  ·  2014-11-25 14:04:13 +08:00
    我靠,好难啊,贴的代码不见了。。。。就是那个空的。。。
    20 条回复    2014-11-25 19:29:57 +08:00
    metas
        1
    metas  
    OP
       2014-11-25 09:20:02 +08:00
    空格自动没了。。。没了。。
    tankb52
        2
    tankb52  
       2014-11-25 09:29:56 +08:00
    不能贴代码吗?
    tankb52
        3
    tankb52  
       2014-11-25 09:31:19 +08:00
    http://www.v2ex.com/t/74249?p=2

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

    改成while True:
    huigeer
        8
    huigeer  
       2014-11-25 10:28:23 +08:00
    楼上真相帝
    metas
        9
    metas  
    OP
       2014-11-25 12:46:00 +08:00
    @tankb52 不好意思啊,我打不开。。
    metas
        10
    metas  
    OP
       2014-11-25 12:46:38 +08:00
    @letv 已改,还是报错
    metas
        11
    metas  
    OP
       2014-11-25 12:55:14 +08:00
    metas
        12
    metas  
    OP
       2014-11-25 12:57:00 +08:00
    @tankb52 代码在下面
    metas
        13
    metas  
    OP
       2014-11-25 12:58:05 +08:00
    @lcqtdwj 已贴,代码在下面,麻烦了
    jsuper
        14
    jsuper  
       2014-11-25 13:17:54 +08:00
    @metas 没看到代码贴在哪儿了。。
    metas
        15
    metas  
    OP
       2014-11-25 13:30:16 +08:00
    @jsuper 中间不是有一个空的吗。。。刚才我贴在那里了。。。。现在不见了
    Sylv
        16
    Sylv  
       2014-11-25 14:34:04 +08:00
    你的代码我试了在 Mac 下运行正常,看报错应该是 database.dat 文件的问题,把 C:\\database.dat 删了再运行试下吧
    metas
        17
    metas  
    OP
       2014-11-25 15:43:57 +08:00
    @Sylv 我在kali下运行了还是报错
    letv
        18
    letv  
       2014-11-25 17:24:57 +08:00
    @metas 把 C:\\database.dat 删了,改成database.dat,顺便在你的源文件目录下新建一个database.dat试试
    metas
        19
    metas  
    OP
       2014-11-25 17:55:47 +08:00
    @letv 你的意思是代码没有问题吗,就是那个 C:\\database.dat 有问题吗?
    aaaa007cn
        20
    aaaa007cn  
       2014-11-25 19:29:57 +08:00   ❤️ 1
    @metas 可以运行,没有大问题
    @letv 的意思可能是你没注意到系统的权限问题
    比如在 win7 下并且 c 盘是系统盘
    不给 python 提权(以管理员权限运行)默认是没法往系统盘 c 盘根目录写文件的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2823 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:22 · PVG 17:22 · LAX 02:22 · JFK 05:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.