我是大学 IT 的一年级学生,我们的教授要求我们制作一个数字系统转换器。我们不允许使用任何会自动转换为数字系统的方法。
我使用此代码来验证二进制输入:
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 以上的其他数字系统,我只是添加了缺少的验证。
例如在 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
我只是添加到每个验证的结尾。但出于某种原因,它要求验证所有数字或允许的数字之一的重复。
输入输出:
Allowed numbers are "0-2", Enter your whole number: 20
Allowed numbers are "0-2", Enter your whole number: 111
['1', '1', '1']
The decimal value of " 111 " base- 3 is 13
Allowed numbers are "0-2", Enter your whole number: 211
Allowed numbers are "0-2", Enter your whole number: 210
['2', '1', '0']
The decimal value of " 210 " base- 3 is 21
将不胜感激任何帮助 <3
慕盖茨4494581
相关分类