Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-KeyError:';x';_Python_Python 2.7_Dictionary_Keyerror - Fatal编程技术网

Python-KeyError:';x';

Python-KeyError:';x';,python,python-2.7,dictionary,keyerror,Python,Python 2.7,Dictionary,Keyerror,这是我的号码2文本代码: 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: p

这是我的号码2文本代码:

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
。我收到此错误
keyrerror:'2'

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


谢谢大家!

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

请注意,错误消息说缺少的键是
'2'
,换句话说,是字符串。您将整数作为键,如
2
。您可以首先将键创建为字符串,也可以将字符串数据转换为整数进行查找。

它的意思是keyrerror:'2'。 我想你搞错了。 您的号码2文本代码错误。 而不是'TEN[NUM(0)]考虑“<强> TEN[INT](STR(num)[0 ] ] < <强> >/p> 因此,如果你把这两个文本放在一起,你会得到:

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]]
它说的是KeyError:'2'。 我想你搞错了。 您的号码2文本代码错误。 而不是'TEN[NUM(0)]考虑“<强> TEN[INT](STR(num)[0 ] ] < <强> >/p> 因此,如果你把这两个文本放在一起,你会得到:

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]]

我做了
num\u I=int(num)
,但当我试图输入
34
时,它告诉我
TypeError:“int”对象没有属性“\uu getitem\uuuuu”
,然后你没有做
int(num)
,你做了
int[num]
(我想).而且我知道我仍然没有做
p==3
之后的所有事情。这不是一个聊天的好地方。如果num是int,则不能执行
num[i]
。我的代码中没有
num[i]
。。。我只是在问:我如何修复
TypeError:“int”对象没有属性“\uu getitem\uuuuu”
我做了
num\u I=int(num)
,但当我试图输入
34
时,它告诉我
TypeError:“int”对象没有属性“\uu getitem\uuuuuuuu”
,然后你没有做
int(num)
,你做了
int[num]
(我想).而且我知道我仍然没有做
p==3
之后的所有事情。这不是一个聊天的好地方。如果num是int,则不能执行
num[i]
。我的代码中没有
num[i]
。。。我只是问:如何修复
TypeError:'int'对象没有属性'\uu getitem'