num_dict = {0:'0', 1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F'}
symbol_dict = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15}
### Number Conversion Program####
def Dec_to_Y(num, M, N):
i = 0
new_num = 0
new_str = ''
while True:
if num < N:
new_num += num * (10 ** i)
new_str = num_dict[num] + new_str
# return (new_num, new_str)
return (new_str)
else:
A = num % N
new_num += A * 10 ** i
new_str = num_dict[A] + new_str
num = num // N
i += 1
def X_to_Dec(num, M, N):
num = str(num)
temp = 0
num_len = len(num)
j = 1
for i in num:
temp += symbol_dict[i] * (M ** (num_len - j))
j += 1
return (temp, M, N)
def X_to_Y(num, M, N):
a = X_to_Dec(num, M, N)
b = Dec_to_Y(*a)
return b
##########################################
###### number input and number check #######
def num_input():
num, M, N = map(lambda x: int(x), input('Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma:').split(','))
a = num, M, N
num_check(*a)
return a
def num_check(num, M, N):
try:
if M == 2:
for i in str(num):
if int(i) >= 2:
raise ValueError
else:
pass
elif M == 8:
for i in str(num):
if int(i) >= 8:
raise ValueError
else:
pass
else:
pass
except ValueError:
print('The number is not in right format')
num_input()
##############################################
a = num_input()
result = X_to_Y(*a)
print('The final number is:' + result)