NumeralSystemConverter.py released

This commit is contained in:
BarsTiger
2021-09-09 21:50:42 +03:00
parent c335d8f277
commit 06c9c3fe17

View File

@@ -1,6 +1,7 @@
print("©KOTIKOT, script by BarsTiger")
print()
print("Please, type only int numbers, str is allowed only in hex")
print("To change system, press Ctrl+C or close window and rerun program with new settings")
hextonum = {
"0": 0,
@@ -21,6 +22,13 @@ hextonum = {
"F": 15
}
def getkeybyval(val, dictionary):
keys = []
for key in list(dictionary):
if dictionary[key] == val:
keys.append(key)
return keys
systypefrom = int(input("Type base number of system convert FROM: "))
systypeto = int(input("Type base number of system convert TO: "))
if systypefrom > 16 or systypeto > 16:
@@ -38,7 +46,23 @@ def todecimal(number, base):
return res
def fromdecimal(number, base):
remainders = []
result = str()
while number != 0:
remainders.insert(0, number % base)
number = number // base
for symbol in remainders:
result += getkeybyval(int(symbol), hextonum)[0]
return result
while True:
inputnumber = input("Type number, that you want to convert: ")
inputnumber = inputnumber.upper()
print(todecimal(inputnumber, systypefrom))
numtoprint = fromdecimal(todecimal(inputnumber, systypefrom), systypeto)
print(numtoprint)