Skip to content

Commit 961dd6c

Browse files
committed
Updated the converter_script.py
1 parent 669a52e commit 961dd6c

File tree

1 file changed

+13
-34
lines changed

1 file changed

+13
-34
lines changed

Number Conversion/converter_script.py

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
class Definations:
23
'''Contains the actual logic for base conversion.
34
@@ -40,6 +41,10 @@ class Converter(Definations):
4041
'''Inherits the Definations Class and converts the number based on user input'''
4142
def __init__(self,number):
4243
super().__init__(number)
44+
45+
def helper(self, func_name):
46+
return getattr(Definations,func_name)(self)
47+
4348
def convert(self,FROM="d",TO="b"):
4449
'''
4550
By Default conversion takes place from decimal to binary.
@@ -49,36 +54,9 @@ def convert(self,FROM="d",TO="b"):
4954
x-hexadecimal,
5055
o-octal
5156
'''
52-
bases = {'d':"DECIMAL",'b':"BINARY",'x':'HEXADECIMAL','o':"OCTAL"}
53-
if FROM == 'd':
54-
if TO == 'b':
55-
ans = Definations.Decimal_to_Binary(self)
56-
elif TO == 'x':
57-
ans = Definations.Decimal_to_Hexadecimal(self)
58-
elif TO == 'o':
59-
ans = Definations.Decimal_to_Octal(self)
60-
if FROM == 'b':
61-
if TO == 'd':
62-
ans = Definations.Binary_to_Decimal(self)
63-
elif TO == 'x':
64-
ans = Definations.Binary_to_Hexadecimal(self)
65-
elif TO == 'o':
66-
ans = Definations.Binary_to_Octal(self)
67-
if FROM == 'x':
68-
if TO == 'b':
69-
ans = Definations.Hexadecimal_to_Binary(self)
70-
elif TO == 'd':
71-
ans = Definations.Hexadecimal_to_Decimal(self)
72-
elif TO == 'o':
73-
ans = Definations.Hexadecimal_to_Octal(self)
74-
if FROM == 'o':
75-
if TO == 'b':
76-
ans = Definations.Octal_to_Binary(self)
77-
elif TO == 'd':
78-
ans = Definations.Octal_to_Decimal(self)
79-
elif TO == 'x':
80-
ans = Definations.Octal_to_Hexadecimal(self)
81-
return f"\n{self.number} in {bases[FROM]} = {ans} in {bases[TO]}"
57+
bases = {'d':"Decimal",'b':"Binary",'x':'Hexadecimal','o':"Octal"}
58+
to_call_function = bases[FROM] + '_to_' + bases[TO]
59+
return f"\n{self.number} in {bases[FROM]} = {self.helper(to_call_function)} in {bases[TO]}"
8260

8361
def header_decoration():
8462
print('''
@@ -90,7 +68,8 @@ def footer_decoration():
9068
-------------- --------------------------- -----------
9169
''')
9270

93-
header_decoration()
94-
Num = Converter(input("Enter number = "))
95-
print(Num.convert(input("From = "),input("To = ")))
96-
footer_decoration()
71+
if __name__=='__main__':
72+
header_decoration()
73+
Num = Converter(input("Enter number = "))
74+
print(Num.convert(input("From = "),input("To = ")))
75+
footer_decoration()

0 commit comments

Comments
 (0)