-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultitronic_base_converter.py
More file actions
44 lines (40 loc) · 1.78 KB
/
multitronic_base_converter.py
File metadata and controls
44 lines (40 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#Converts a user-defined number from one user-defined base to another
#Highest base determined by number of possible number symbols as defined in LOOKUP
#Builds a dictionary using enumerate function on chosen order of chosen number symbols
LOOKUP = {b: i for i, b in enumerate('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')}
_LOOKUP = {b: i for i, b in LOOKUP.items()}
#Function = 2 part process, converts user number to denary (if not already) and then from denary to specified base
#Multipliers, factors and operands derived from user-specified base values
def convert(inputnum, frombase, tobase):
"""
>>> convert('0', 2, 10)
'0'
>>> convert('1100', 2, 10)
'12'
>>> convert('12', 10, 2)
'1100'
>>> convert('AA', 16, 10)
'170'
"""
decsum = 0
#Cconverts any extra symbols back to decimal numerals and reverses the input number
decval = (LOOKUP[i] for i in reversed(inputnum))
#Converts user number to denary
for i, j in enumerate(decval):
decsum += j * (frombase**i)
output = ""
#Converts (possibly new) denary number to specified base
while decsum > 0:
remainder = decsum % (tobase)
output += str(_LOOKUP[remainder])
decsum = int((decsum - remainder) / (tobase))
if not output:
output = '0'
return output
if __name__ == '__main__':
#Creates key variables from user input (base values converted to integers to be called later)
inputnum = (input('Enter your number: '))
frombase = int(input('In which base is your number?: '))
tobase = int(input('To which base would you like to convert it?: '))
#Prefaces output with user input and calls main function based on input
print(f"Your base {frombase} number {inputnum} in base {tobase} = {convert(inputnum, frombase, tobase)}")