我有这段代码
ones = {1 : "I", 2 : "II", 3 : "III", 4 : "IV", 5: "V", 6 : "VI",\
7 : "VII", 8 : "VIII", 9 : "IX"}
tens = {10 : "X", 20 : "XL", 30 : "XXX", 40 : "XL", 50 : "L",\
60 : "LX", 70 : "LXX", 80 : "LXXX", 90 : "XC"}
hun = {100 : "C", 200 : "CC", 300 : "CCC"}
ui = input('type a number from 1 to 303: ')
k = ui
print(ones[k])
我试图让用户输入1到399之间的数字。
当我使用ones[ui]
时,我得到了一个
KeyError'2'
2是我输入的数字。所以,我将ui
的输入传递给另一个名为k
的变量,但我遇到了同样的问题。
在python的实时环境中(空闲),当我写ones[2]
时,我得到"II"
而不是上面的错误。
那么,究竟是什么问题,我该如何解决呢?
答案 0 :(得分:7)
这是因为您的输入是string
,而不是int
(使用python 3),因此您实际上正在执行失败的ones["2"]
。
ui = int(input('type a number from 1 to 303: '))
将解决这个问题。
请注意,如果用户输入" 303"你也会得到一个关键错误。最好为你的例子做print(ones[ui%10])
(你还必须检查!= 0,因为罗马数字不支持它:))
除20 : "XL",
之外应该是20 : "XX",
我的修复提案完全实现了数字构建(也适用于python 2):
ones = {1 : "I", 2 : "II", 3 : "III", 4 : "IV", 5: "V", 6 : "VI",
7 : "VII", 8 : "VIII", 9 : "IX",0:""}
tens = {10 : "X", 20 : "XX", 30 : "XXX", 40 : "XL", 50 : "L",
60 : "LX", 70 : "LXX", 80 : "LXXX", 90 : "XC",0:""}
hun = {100 : "C", 200 : "CC", 300 : "CCC",0:""}
ui = int(input('type a number from 1 to 303: '))
if 0 < ui < 304:
units = ui%10
tenths = (ui-units)%100
hundreds = (ui-tenths-units)
string = "".join([d[v] for d,v in zip((hun,tens,ones),(hundreds,tenths,units))])
print(string)
答案 1 :(得分:4)
input
的文档已经解释了这一点:
然后该函数从输入中读取一行,将其转换为字符串(剥离尾随换行符),然后返回该行。
所以你的ui
是一个字符串,但字典中的键是数字。因此找不到任何匹配项(因为2 != '2'
)并生成KeyError
。
正如@ Jean-FrançoisFabre已经提到的,你需要将它转换为整数:
ui = input('type a number from 1 to 303: ')
if len(ui) == 3:
ones_ui = int(ui[2])
tens_ui = int(ui[1])
hund_ui = int(ui[0])
elif len(ui) == 2:
ones_ui = int(ui[1])
tens_ui = int(ui[0])
elif len(ui) == 1:
ones_ui = int(ui[0])
else:
print('ups')
或者更好地反转字符串,然后将每个数字映射到整数:
ui = input('type a number from 1 to 303: ')
nums = list(map(int, ui[::-1]))
例如,输入20
会返回[0, 2]
如果你使用索引,那么你需要稍微改变你的词典:
ones = {1 : "I", 2 : "II", 3 : "III", 4 : "IV", 5: "V", 6 : "VI", 7 : "VII", 8 : "VIII", 9 : "IX", 0: ""}
tens = {1 : "X", 2 : "XL", 3 : "XXX", 4 : "XL", 5 : "L", 6 : "LX", 7 : "LXX", 8 : "LXXX", 9 : "XC", 0: ""}
hun = {1 : "C", 2 : "CC", 3 : "CCC", 0: ""}
我将数字0
添加到所有这些中,因为那时你不需要特殊情况,你可以抛出这些:
print(''.join([dct[val] for val, dct in zip(nums, (ones, tens, hun))][::-1]))
[::-1]
在连接所有部分之前再次反转生成的字符串。
样品:
type a number from 1 to 303: 10
X
type a number from 1 to 303: 303
CCCIII
type a number from 1 to 303: 158
CLVIII
type a number from 1 to 303: 19
XIX
我使用的代码是:
ones = {1 : "I", 2 : "II", 3 : "III", 4 : "IV", 5: "V", 6 : "VI", 7 : "VII", 8 : "VIII", 9 : "IX", 0: ""}
tens = {1 : "X", 2 : "XL", 3 : "XXX", 4 : "XL", 5 : "L", 6 : "LX", 7 : "LXX", 8 : "LXXX", 9 : "XC", 0: ""}
hun = {1 : "C", 2 : "CC", 3 : "CCC", 0: ""}
ui = input('type a number from 1 to 303: ')
nums = list(map(int, ui[::-1]))
print(''.join([dct[val] for val, dct in zip(nums, (ones, tens, hun))][::-1]))
答案 2 :(得分:1)
ones = {1 : "I", 2 : "II", 3 : "III", 4 : "IV", 5: "V", 6 : "VI",\
7 : "VII", 8 : "VIII", 9 : "IX"}
tens = {10 : "X", 20 : "XX", 30 : "XXX", 40 : "XL", 50 : "L",\
60 : "LX", 70 : "LXX", 80 : "LXXX", 90 : "XC",0:""}
hun = {100 : "C", 200 : "CC", 300 : "CCC",0:""}
MAPPING = {3:[hun,tens,ones],
2:[tens, ones],
1:[ones]}
rr = 304
_input = str(input('type a number from 1 to {0}: '.format(rr)))
_out = []
#check range and schema
if 0<int(_input)<rr and MAPPING.has_key(len(_input)):
for n,sub_dict in enumerate(MAPPING[len(_input)]):
#ignore zero
if int(_input[n]):
# go by mapping values, based on input len
_out.append([sub_dict[x] for x in sub_dict.keys() if _input[n] in str(x)])
print _out
>>>type a number from 1 to 304: 201
>>>[['CC'], ['I']]
>>>type a number from 1 to 304: 74
>>>[['LXX'], ['IV']]
>>>type a number from 1 to 304: 303
>>>[['CCC'], ['III']]
>>>type a number from 1 to 304: 123
>>>[['C'], ['XX'], ['III']]