2017-01-27 60 views
1

我有這樣的代碼如何返回密鑰的多字典值?

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"代替上述錯誤。

那麼,究竟是什麼問題,我該如何解決呢?

+1

也許我們應該適應問題的「稱號」 - 因爲它已經成爲誤導,現在的答案你清除了霧;-) – Dilettant

+1

@Dilettant完成 –

回答

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) 
+0

你還需要爲'ones'添加'0:''',否則'10'不起作用:-)另外,你不需要反斜槓,在字典中打破。 – MSeifert

4

input的文檔已經說明了這一點:

該函數然後從輸入中讀取一行,將其轉換爲字符串(剝離尾隨換行符),並返回該行。

所以你的ui是一個字符串,但你的字典中的鍵是數字。所以它找不到任何匹配(因爲2 != '2')並且產生了KeyError。已經通過@讓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])) 
+0

在訪問'ui [2]'之前,你必須檢查長度,甚至檢查它是否是一個整數。雖然索引爲+1。 –

+0

@ Jean-FrançoisFabre再次感謝。 – MSeifert

+0

當然還有「0」的情況,這是羅馬數字未處理,只是迂腐:) –

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']] 
相關問題