def replace_base(x):
x = x.replace('10', 'A')
x = x.replace('11', 'B')
x = x.replace('12', 'C')
x = x.replace('13', 'D')
x = x.replace('14', 'E')
x = x.replace('15', 'F')
x = x.replace('16', 'G')
return x
def deci_to_any_list():
n1 = input("Enter a number: ")
n = int(n1)
radix1 = input("Enter a radix: ")
radix = int(radix1)
converted_number = []
if n>0:
if 2<=radix<=16:
while int(n/radix) == 0:
converted_number.append(n % radix)
converted_number.reverse()
x = ''.join(str(i) for i in converted_number)
replace_base(x)
else:
converted_number.append(n % radix)
else:
print("Wrong input!!")
else:
print("Wrong input!!")
print("%d in base 10 is %d in base %d" % (n1,x,radix1))
deci_to_any_list()
Input & error,
Enter a number: 61
Enter a radix: 16
Traceback (most recent call last):
File "C:/Users/LG/기컴프과제2/Number conversion.py", line 33, in <module>
deci_to_any_list()
File "C:/Users/LG/기컴프과제2/Number conversion.py", line 31, in deci_to_any_list
print("%d in base 10 is %d in base %d" % (n1,x,radix1))
UnboundLocalError: local variable 'x' referenced before assignment
Process finished with exit code 1
I want to create a decimal transformation function. I posted a question earlier, but you asked me to give you more details, so I'm leaving a question again. I don't know how to fix the error, and I don't know how to make this function. It can make to divide the quotient by the radix base number until the quotient is 0. And, write remainders in reverse order. I want to make this function using the append, reverse,join function,formatting operator(%). At this time, n shall be positive number and radix shall be 2 to 16. The last result is '61 in base 10 is 3D in base 16'.