I want o create a calculator that can add (and multiply, divide, etc) numbers in base 12 and with different limits at the different digits.
Base 12 sequence: [0,1,2,3,4,5,6,7,8,9,"A","B"]
The limits must be:
First digit: limit "B"
Second digit: limit 4
Third digit: limit "B"
(The idea would be that it follows the hourly System limits but in base 12 so for example in base 12 there are 50 seconds in a minute)
That means you would count like this:[1,2,3,4,5,6,7,8,9,A,B,10,11,...48,49,4A,4B,100,101,...14B,200,201,...B4B,1000,1001..]
So I made the following code
import string
digs = string.digits + string.ascii_uppercase
def converter(number):
#split number in figures
figures = [int(i,12) for i in str(number)]
#invert oder of figures (lowest count first)
figures = figures[::-1]
result = 0
#loop over all figures
for i in range(len(figures)):
#add the contirbution of the i-th figure
result += figures[i]*12**i
return result
def int2base(x):
if x < 0:
sign = -1
elif x == 0:
return digs[0]
else:sign = 1
x *= sign
digits = []
while x:
digits.append(digs[int(x % 12)])
x = int(x / 12)
if sign < 0:
digits.append('-')
digits.reverse()
return ''.join(digits)
def calculator (entry1, operation, entry2):
value1=float(converter(entry1))
value2=float(converter(entry2))
if operation == "suma" or "+":
resultvalue=value1+value2
else:
print("operación no encontrada, porfavor ingrese +,-,")
result=int2base(resultvalue)
return result
print(calculator(input("Ingrese primer valor"), input("ingrese operación"), input("Ingrese segundo valor")))
The thing is that I dont know how to establish the limits to the different digits
If someone could help me I would be extreamly greatful
See Question&Answers more detail:
os