Reputation: 33
This is my number2text code:
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]]
And these are my dictionaries:
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'})
If my input is 32
for example. I am getting this error KeyError: '2'
I saw some other posts, but they were only about KeyError: x
(without the ''
), and the ones that did have ''
, were only because someone forgot to put an x
key in the dict.
Thank You!
Upvotes: 1
Views: 12384
Reputation: 212
It's saying KeyError: '2'. I think you got mixed up. Your number2text code is wrong. Instead of 'tens[num[0]]' consider 'tens[int(str(num)[0])]'
so if you put the two texts together you get:
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]]
Upvotes: 1
Reputation: 375814
Notice that the error message says the missing key is '2'
, in other words, a string. You have integers as keys, like 2
. You can either create your keys as strings to begin with, or convert your string data into ints to do the lookup.
Upvotes: 1