【问题标题】:Why Am I getting this key error? [closed]为什么我会收到这个关键错误? [关闭]
【发布时间】:2013-12-04 23:15:26
【问题描述】:

我不知道我在这里做错了什么,但我不断收到一个关键错误,不知道为什么,我错过了什么?

campers = {'pb' : 'Pooder Bennet', 'jf' : 'Jupiter Fargo',
           'rb' : 'Randy Buffet', 'bl' : 'Botany Lynn',
       'bt' : 'Boris Tortavich', 'tn' : 'Trinda Noober',
       'fj' : 'Freetus Jaunders', 'nt' : 'Ninar Tetris', 
       'gm' : 'Gloobin Marfo', 'nk' : 'Niche Kaguya',
       'bd' : 'Brent Drago', 'vt' : 'Volga Toober',
       'kt' : 'Kinser Talebearing', 'br' : 'Bnola Rae',
       'nb' : 'Nugget Beano', 'yk' : 'Yeldstat Krong',
       'gy' : 'Gelliot Yabelor', 'il' : 'Illetia Dorfson',
       'ct' : 'Can Tabber', 'tv' : 'Trinoba Vyder'}

    campers_outside_theater = random.sample(campers.keys(), 5)
    people = campers_outside_theater + ['Troid, the counselor from the bus.']
    choices = '\n\n'.join('%d. %s' % (i + 1, campers[p]) for (i, p) in enumerate(people))

【问题讨论】:

  • 我们应该猜出什么关键错误吗?
  • 你能提供给我们回溯吗?
  • 对不起,我是新手,如何从 windows powershell 复制回溯??
  • @lerugray 你在哪里看到错误,复制/粘贴到这里
  • 如果您使用的是 Python 3,则 dict keys 返回一个视图。 random.sample 需要一个序列。使用list(campers) 而不是campers.keys()。 (不过,我怀疑如果这样做,您会立即遇到下一个错误。)不过,如果这是问题所在,我认为您会得到 TypeError 而不是 KeyError。

标签: python dictionary keyerror


【解决方案1】:

这会给你几乎你想要的:

import random
campers = {'pb' : 'Pooder Bennet', 'jf' : 'Jupiter Fargo',
           'rb' : 'Randy Buffet', 'bl' : 'Botany Lynn',
       'bt' : 'Boris Tortavich', 'tn' : 'Trinda Noober',
       'fj' : 'Freetus Jaunders', 'nt' : 'Ninar Tetris', 
       'gm' : 'Gloobin Marfo', 'nk' : 'Niche Kaguya',
       'bd' : 'Brent Drago', 'vt' : 'Volga Toober',
       'kt' : 'Kinser Talebearing', 'br' : 'Bnola Rae',
       'nb' : 'Nugget Beano', 'yk' : 'Yeldstat Krong',
       'gy' : 'Gelliot Yabelor', 'il' : 'Illetia Dorfson',
       'ct' : 'Can Tabber', 'tv' : 'Trinoba Vyder'}

campers_outside_theater = random.sample(campers.keys(), 5)
people = campers_outside_theater #+ ['Troid, the counselor from the bus.']
choices = '\n\n'.join('%d. %s' % (i + 1, campers[p]) for (i, p) in enumerate(people))
print(choices)

你有keys(people),但没有这样的动物——那是你的第一个错误。它不是KeyError,而是NameError(因为从未定义过keys)。然后,当我删除密钥并且只有 enumerate(people) 时,您得到了一个实际的密钥错误,因为您试图使用 'Troid, the counselor from the bus.' 作为密钥......但它不是一个。我假设您想将他包括在公共汽车上的人员中,但是您必须以不同的方式进行。也许将他包含在您的露营者字典中,并在您随机抽样后始终将他添加到您的钥匙中。

【讨论】:

  • 我能问一下#+在做什么吗?那是蟒蛇3吗?以前没见过。
  • 注释掉#之后的所有内容。如果取消注释,您将收到错误消息,因为露营者中没有名为 Troid, the ... 的密钥。正如我上面所说,您应该考虑以某种方式将他添加到字典中以保持内容整洁。
  • 我明白了,这是我的代码中的第一个错误!哈哈,我得弄清楚如何让特定的非露营 npc 出现,也许是所有字符的字典 :)
【解决方案2】:

这行是导致您的错误的原因:

choices = '\n\n'.join('%d. %s' % (i + 1, campers[p]) for (i, p) in enumerate(people))

原因是因为这行:

people = campers_outside_theater + ['Troid, the counselor from the bus.']

字典里没有名字campersTroid, the counselor from the bus.

解决这个问题:

>>> campers.update([('Troid', 'the counselor from the bus.')])

【讨论】:

    猜你喜欢
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2014-10-01
    • 1970-01-01
    • 2015-10-05
    • 2022-07-01
    相关资源
    最近更新 更多