def exp_in(n, g): ret = 1 while n ** ret < g: ret += 1 return ret - 1
def base(n, to_base): ''' 2 <= to_base <= 16 ''' if not 2 <= to_base <= 16: raise ValueError('2 <= to_base <= 16') ret = '' for e in range(exp_in(to_base, n), -1, -1): t = to_base ** e c = n // t ret += sym.get(c, str(c)) n %= t return ret
n, b = [int(input(x)) for x in ['Число: ', 'Степень (2 <= n <= 16): ']] print('{} (10) = {} ({})'.format(n, base(n, b), b))
Answers & Comments
sym = {10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F'}
def exp_in(n, g):
ret = 1
while n ** ret < g:
ret += 1
return ret - 1
def base(n, to_base):
''' 2 <= to_base <= 16 '''
if not 2 <= to_base <= 16:
raise ValueError('2 <= to_base <= 16')
ret = ''
for e in range(exp_in(to_base, n), -1, -1):
t = to_base ** e
c = n // t
ret += sym.get(c, str(c))
n %= t
return ret
n, b = [int(input(x)) for x in ['Число: ', 'Степень (2 <= n <= 16): ']]
print('{} (10) = {} ({})'.format(n, base(n, b), b))