1- # Import Modules
1+ # Author: @sleepy4k
2+ # License: MIT License
3+ # Description: A simple number convertion application with Python.
24import os
35from enum import Enum
46
5- # Class
7+ # Enumerate the base numbers.
68class Base (Enum ):
79 DECIMAL = 1
810 BINARY = 2
911 OCTAL = 3
1012 HEXADECIMAL = 4
1113 ASCII = 5
1214
13- # Clear Command
15+ # Clears the command prompt.
1416def clear_cmd ():
1517 os .system ("cls" ) if os .name == "nt" else os .system ("clear" )
1618
17- # separator
19+ # Prints a separator line.
1820def separator ():
1921 print ("-" * 30 )
2022
21- # Title
23+ # Prints the application title.
2224def title ():
2325 clear_cmd ()
2426 separator ()
25- print ("Number Convertion with Python" )
27+ print ("Simple Number Convertion with Python" )
2628 separator ()
2729
28- # Menu
30+ # Prints the menu options.
2931def menu ():
3032 title ()
3133 for base in Base :
3234 print (f"{ base .value } . { base .name } " )
33- print (f"{ len (Base )+ 1 } . Exit" )
35+ print (f"{ len (Base ) + 1 } . Exit" )
3436 separator ()
3537
36- # Get Input
38+ # Prompts the user for input data.
3739def get_input (base ):
3840 title ()
39- data = input (f"Input your { base .name .lower ()} number : " )
41+ data = input (f"Input your { base .name .lower ()} number or word : " )
4042 if not data :
4143 raise ValueError (f"{ base .name .lower ()} number is required" )
4244 return data
4345
44- # Error
46+ # Handles errors and prompts the user to continue or exit.
4547def error (err ):
4648 separator ()
47- print (err )
49+ print (f"System error : { err } " )
4850 separator ()
4951 choice = input ("Do you want to continue? [yes/no] : " )
5052 if choice .lower () in ["yes" , "y" ]:
51- logic ()
53+ return True
5254 elif choice .lower () in ["no" , "n" ]:
53- exit ()
55+ logic ()
5456 else :
55- error ("Invalid choice" )
57+ return error ("Invalid choice" )
5658
57- # Convertion
59+ # Converts the input data to decimal and other bases.
5860def convertion (base , data ):
5961 try :
6062 match base :
@@ -67,31 +69,39 @@ def convertion(base, data):
6769 case Base .HEXADECIMAL :
6870 decimal = int (data , 16 )
6971 case Base .ASCII :
72+ """Convert string to array."""
7073 decimal = [ord (character ) for character in data ]
71- binary = '' .join ([bin (character )[2 :] for character in decimal ])
72- octal = '' .join ([oct (character )[2 :] for character in decimal ])
73- hexadecimal = '' .join ([hex (character )[2 :] for character in decimal ])
74+
75+ """Convert array to other bases."""
76+ binary = ' ' .join ([bin (character )[2 :] for character in decimal ])
77+ octal = ' ' .join ([oct (character )[2 :] for character in decimal ])
78+ hexadecimal = ' ' .join ([hex (character )[2 :] for character in decimal ])
7479 asci = '' .join ([chr (character ) for character in decimal ])
7580
76- # Convert to string
77- decimal = '' .join ([str (character ) for character in decimal ])
78- return (decimal , binary , octal , hexadecimal , asci )
79- case _:
80- error ("Invalid base" )
81+ """Convert array to decimal."""
82+ decimal = ' ' .join ([str (character ) for character in decimal ])
83+
84+ """Return the result."""
85+ return [decimal , binary , octal , hexadecimal , asci ]
86+ """Convert decimal to other bases."""
8187 binary = bin (decimal )[2 :]
8288 octal = oct (decimal )[2 :]
8389 hexadecimal = hex (decimal )[2 :]
8490 asci = chr (decimal )
85- return (decimal , binary , octal , hexadecimal , asci )
86- except ValueError :
87- error ("System has value error" )
88- except TypeError :
89- error ("System has data type error" )
91+
92+ """Return the result."""
93+ return [decimal , binary , octal , hexadecimal , asci ]
94+ except ValueError as valErr :
95+ error (valErr )
96+ except TypeError as typeErr :
97+ error (typeErr )
9098 except Exception as err :
91- error (f"System error: { err } " )
99+ error (err )
100+ """Return the result."""
101+ return main (base )
92102
93- # Confirmation
94- def confirmation (base ):
103+ # Prompts the user to retry or exit.
104+ def retry (base ):
95105 choice = input ("Do you want to try again? [yes/no] : " )
96106 separator ()
97107 if choice .lower () in ["yes" , "y" ]:
@@ -100,35 +110,36 @@ def confirmation(base):
100110 logic ()
101111 else :
102112 error ("Invalid choice" )
113+ return retry (base )
103114
104- # Result
105- def result (decimal , binary , octal , hexadecimal , asci ):
106- separator ()
107- print (f"Decimal : { decimal } " )
108- print (f"Binary : { binary } " )
109- print (f"Octal : { octal } " )
110- print (f"Hexadecimal : { hexadecimal } " )
111- print (f"ASCII : { asci } " )
112- separator ()
113-
114- # Main
115+ # Main logic.
115116def main (base ):
116- data = get_input (base )
117- decimal , binary , octal , hexadecimal , asci = convertion (base , data )
118- result (decimal , binary , octal , hexadecimal , asci )
119- confirmation (base )
117+ try :
118+ data = get_input (base )
119+ conversions = convertion (base , data )
120+ separator ()
121+ print (f"Decimal : { conversions [0 ]} " )
122+ print (f"Binary : { conversions [1 ]} " )
123+ print (f"Octal : { conversions [2 ]} " )
124+ print (f"Hexadecimal : { conversions [3 ]} " )
125+ print (f"ASCII : { conversions [4 ]} " )
126+ separator ()
127+ retry (base )
128+ except ValueError as err :
129+ error (err )
130+ main (base )
120131
121- # Logic
132+ # Initial Logic
122133def logic ():
123134 menu ()
124135 select = input ('Select your output : ' )
125136 separator ()
126- if select == str (len (Base )+ 1 ):
137+ if select == str (len (Base ) + 1 ):
127138 exit ()
128139 elif select not in [str (base .value ) for base in Base ]:
129140 error ("Invalid choice" )
130141 else :
131142 main (Base (int (select )))
132143
133- # Run
144+ # Run the application.
134145logic ()
0 commit comments