我是大學(xué) IT 的一年級學(xué)生,我們的教授要求我們制作一個(gè)數(shù)字系統(tǒng)轉(zhuǎn)換器。我們不允許使用任何會(huì)自動(dòng)轉(zhuǎn)換為數(shù)字系統(tǒng)的方法。我使用此代碼來驗(yàn)證二進(jìn)制輸入: def base2(): s = {'0','1'} y = input('Allowed numbers are "0-1", Enter your whole number: ') val = set(y) while not (s == val or val == {'0'} or val == {'1'}): y = input('Allowed numbers are "0-1", Enter your whole number: ') val = set(y) return y對于 base-2 以上的其他數(shù)字系統(tǒng),我只是添加了缺少的驗(yàn)證。例如在 base-3 中:def base3(): s = {'0','1','2'} y = input('Allowed numbers are "0-2", Enter your whole number: ') val = set(y) while not (s == val or val == {'0'} or val == {'1'} or val == {'2'}): y = input('Allowed numbers are "0-2", Enter your whole number: ') val = set(y) return y我只是添加到每個(gè)驗(yàn)證的結(jié)尾。但出于某種原因,它要求驗(yàn)證所有數(shù)字或允許的數(shù)字之一的重復(fù)。輸入輸出:Allowed numbers are "0-2", Enter your whole number: 20Allowed numbers are "0-2", Enter your whole number: 111['1', '1', '1']The decimal value of " 111 " base- 3 is 13Allowed numbers are "0-2", Enter your whole number: 211Allowed numbers are "0-2", Enter your whole number: 210['2', '1', '0']The decimal value of " 210 " base- 3 is 21將不勝感激任何幫助 <3
(Python) 驗(yàn)證和輸入適用于二進(jìn)制,但不適用于其他基數(shù)系統(tǒng)
喵喵時(shí)光機(jī)
2023-02-07 13:42:22