【问题标题】:Python - KeyError: 'x'Python - KeyError:'x'
【发布时间】:2017-03-19 01:26:53
【问题描述】:

这是我的 number2text 代码:

num = raw_input "insert an int"
p = len(num) 
if p == 1:
    print numbers[num]
if p == 2:
    print tens[num[0]] + numbers_teeens[num[1]]
if p == 3:
    print numbers_hundreds[num[0]] + tens[num[1]] + numbers_teeens[num[2]]
if p == 4:
print numbers_thousands[num[0]]+ numbers_hundreds[num[1]] + tens[num[2]] + numbers_teeens[num[3]]
if p == 5:
print numbers_ten_thousands[num[0]] + numbers_thousands[num[1]]+ numbers_hundreds[num[2]] + tens[num[3]] + numbers_teeens[num[4]]
if p == 6:
print numbers_hundred_thousands[num[0]] + numbers_ten_thousands[num[1]] + numbers_thousands[num[2]]+ numbers_hundreds[num[3]] + tens[num[4]] + numbers_teeens[num[5]]

这些是我的字典:

numbers = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine'}
tens = {1: 'Ten', 2: 'Twenty', 3: 'Thirty', 4: 'Forty', 5: 'fifty', 6: 'Sixty', 7: 'Seventy', 8: 'Eighty', 9: 'Ninety'}
numbers_teens = {1: 'Eleven', 2: 'Twelve', 3: 'Thirteen', 4: 'Fourteen', 5: 'Fifteen', 6: 'Sixteen', 7: 'Seventeen', 8: 'Eighteen', 9: 'Nineteen'}
numbers_teeens = {0 : '', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen', 19: 'Nineteen'}
numbers_hundreds = {}
numbers_thousands = {}
numbers_ten_thousands = {}
numbers_teen_thousands = {}
numbers_hundred_thousands = {}

#Creates lists
for k,v in numbers.items():
    numbers_hundreds.update({k: v.title() + ' Hundred'}) 
    numbers_thousands.update({k: v.title() + ' Thousand and'})  

for k1,v1 in tens.items():
    numbers_ten_thousands.update({k1: v1.title()})

for kt,vt in numbers_teens.items():
    numbers_teen_thousands.update({kt: vt.title() + ' Thousand'})

for k2,v2 in numbers_hundreds.items():
    numbers_hundred_thousands.update({k2: v2.title() + ' Thousand'}) 

例如,如果我的输入是 32。我收到此错误KeyError: '2'

我看到了其他一些帖子,但它们只是关于KeyError: x(没有''),而那些确实有'' 的帖子只是因为有人忘记在字典中输入x 键.

谢谢!

【问题讨论】:

    标签: python python-2.7 dictionary keyerror


    【解决方案1】:

    请注意,错误消息显示缺少的键是'2',换句话说,是一个字符串。你有整数作为键,比如2。您可以将键创建为字符串开头,也可以将字符串数据转换为整数以进行查找。

    【讨论】:

    • 我输入了num_i = int(num),但是当我尝试输入34时它告诉我TypeError: 'int' object has no attribute '__getitem__'
    • 那你没有做int(num),你做了int[num](我想)。
    • 而且我知道我仍然没有做p ==3之后的所有事情
    • 这不是聊聊您的程序的好地方。如果 num 是 int,则不能使用 num[i]
    • 我的代码中没有num[i]...我只是问:如何修复TypeError: 'int' object has no attribute '__getitem__'
    【解决方案2】:

    这是说 KeyError: '2'。 我想你搞混了。 您的 number2text 代码错误。 而不是 'tens[num[0]]' 考虑 'tens[int(str(num)[0])]'

    所以如果你把这两个文本放在一起,你会得到:

    numbers = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7:           'Seven', 8: 'Eight', 9: 'Nine'}
    tens = {1: 'Ten', 2: 'Twenty', 3: 'Thirty', 4: 'Forty', 5: 'fifty', 6: 'Sixty', 7: 'Seventy', 8: 'Eighty', 9: 'Ninety'}
    numbers_teens = {1: 'Eleven', 2: 'Twelve', 3: 'Thirteen', 4: 'Fourteen', 5: 'Fifteen', 6: 'Sixteen', 7: 'Seventeen', 8: 'Eighteen', 9: 'Nineteen'}
    numbers_teeens = {0 : '', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen', 19: 'Nineteen'}
    numbers_hundreds = {}
    numbers_thousands = {}
    numbers_ten_thousands = {}
    numbers_teen_thousands = {}
    numbers_hundred_thousands = {}
    
    #Creates lists
    for k,v in numbers.items():
        numbers_hundreds.update({k: v.title() + ' Hundred'}) 
        numbers_thousands.update({k: v.title() + ' Thousand and'})  
    
    for k1,v1 in tens.items():
        numbers_ten_thousands.update({k1: v1.title()})
    
    for kt,vt in numbers_teens.items():
        numbers_teen_thousands.update({kt: vt.title() + ' Thousand'})
    
    for k2,v2 in numbers_hundreds.items():
        numbers_hundred_thousands.update({k2: v2.title() + ' Thousand'})
    
    num = raw_input "insert an int"
    p = len(num) 
    if p == 1:
        print int(numbers[int(str(num))]
    if p == 2:
        print tens[int(str(int(str(num)))[0])] + numbers_teeens[int(str(num))[1]]
    if p == 3:
        print numbers_hundreds[int(str(num))[0]] + tens[int(str(num))[1]] + numbers_teeens[int(str(num))[2]]
    if p == 4:
        print numbers_thousands[int(str(num))[0]]+ numbers_hundreds[int(str(num))[1]] + tens[int(str(num))[2]] + numbers_teeens[int(str(num))[3]]
    if p == 5:
        print numbers_ten_thousands[int(str(num))[0]] + numbers_thousands[int(str(num))[1]]+ numbers_hundreds[int(str(num))[2]] + tens[int(str(num))[3]] + numbers_teeens[int(str(num))[4]]
    if p == 6:
        print numbers_hundred_thousands[int(str(num))[0]] + numbers_ten_thousands[int(str(num))[1]] + numbers_thousands[int(str(num))[2]]+ numbers_hundreds[int(str(num))[3]] + tens[int(str(num))[4]] + numbers_teeens[int(str(num))[5]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 2021-04-23
      • 2022-01-21
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多