Skip to content

Commit e506a49

Browse files
committed
chore: main.py
1 parent 017333a commit e506a49

File tree

1 file changed

+46
-72
lines changed

1 file changed

+46
-72
lines changed

main.py

Lines changed: 46 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,72 @@
11
# Import Modules
22
import os
3+
from enum import Enum
4+
5+
# Class
6+
class Base(Enum):
7+
DECIMAL = 1
8+
BINARY = 2
9+
OCTAL = 3
10+
HEXADECIMAL = 4
311

412
# Clear Command
513
def clear_cmd():
6-
os.system('cls') if os.name == 'nt' else os.system('clear')
14+
os.system("cls") if os.name == "nt" else os.system("clear")
715

8-
# Seperator
9-
def seperator():
16+
# separator
17+
def separator():
1018
print("-" * 30)
1119

1220
# Title
1321
def title():
1422
clear_cmd()
15-
seperator()
23+
separator()
1624
print("Number Convertion with Python")
17-
seperator()
25+
separator()
1826

1927
# Menu
2028
def menu():
2129
title()
22-
print("1. Decimal")
23-
print("2. Binary")
24-
print("3. Octal")
25-
print("4. Hexadecimal")
26-
print("5. Exit")
27-
seperator()
30+
for base in Base:
31+
print(f"{base.value}. {base.name}")
32+
print(f"{len(Base)+1}. Exit")
33+
separator()
2834

2935
# Get Input
3036
def get_input(base):
3137
title()
32-
data = input(f"Input your {base} number : ")
38+
data = input(f"Input your {base.name.lower()} number : ")
3339
if not data:
34-
raise ValueError(f"{base} number is required")
40+
raise ValueError(f"{base.name.lower()} number is required")
3541
return data
3642

3743
# Error
3844
def error(err):
39-
seperator()
45+
separator()
4046
print(err)
41-
seperator()
47+
separator()
4248
choice = input("Do you want to continue? [yes/no] : ")
43-
init() if choice.lower() in ["yes", "y"] else exit()
49+
logic() if choice.lower() in ["yes", "y"] else exit()
4450

4551
# Convertion
4652
def convertion(base, data):
4753
try:
48-
if base == 1:
54+
if base == Base.DECIMAL:
4955
decimal = int(data)
5056
binary = bin(decimal)[2:]
5157
octal = oct(decimal)[2:]
5258
hexadecimal = hex(decimal)[2:]
53-
elif base == 2:
59+
elif base == Base.BINARY:
5460
decimal = int(data, 2)
5561
binary = data
5662
octal = oct(decimal)[2:]
5763
hexadecimal = hex(decimal)[2:]
58-
elif base == 3:
64+
elif base == Base.OCTAL:
5965
decimal = int(data, 8)
6066
binary = bin(decimal)[2:]
6167
octal = data
6268
hexadecimal = hex(decimal)[2:]
63-
elif base == 4:
69+
elif base == Base.HEXADECIMAL:
6470
decimal = int(data, 16)
6571
binary = bin(decimal)[2:]
6672
octal = oct(decimal)[2:]
@@ -74,75 +80,43 @@ def convertion(base, data):
7480
error(f"System error: {err}")
7581

7682
# Confirmation
77-
def confirmation(func):
83+
def confirmation(base):
7884
choice = input("Do you want to try again? [yes/no] : ")
79-
seperator()
85+
separator()
8086
if choice.lower() in ["yes", "y"]:
81-
functions = {
82-
'decimal': decimal,
83-
'binary': binary,
84-
'octal': octal,
85-
'hexadecimal': hexadecimal,
86-
}
87-
function = functions.get(func, error)
88-
function()
87+
main(base)
8988
elif choice.lower() in ["no", "n"]:
90-
init()
89+
logic()
9190
else:
92-
error("Invalid choice")
91+
print("Invalid choice")
9392

9493
# Result
9594
def result(decimal, binary, octal, hexadecimal):
96-
seperator()
95+
separator()
9796
print(f"Decimal : {decimal}")
9897
print(f"Binary : {binary}")
9998
print(f"Octal : {octal}")
10099
print(f"Hexadecimal : {hexadecimal}")
101-
seperator()
100+
separator()
102101

103102
# Main
104-
def decimal():
105-
data = get_input("decimal")
106-
decimal, binary, octal, hexadecimal = convertion(1, data)
107-
result(decimal, binary, octal, hexadecimal)
108-
confirmation("decimal")
109-
110-
def binary():
111-
data = get_input("binary")
112-
decimal, binary, octal, hexadecimal = convertion(2, data)
103+
def main(base):
104+
data = get_input(base)
105+
decimal, binary, octal, hexadecimal = convertion(base, data)
113106
result(decimal, binary, octal, hexadecimal)
114-
confirmation("binary")
115-
116-
def octal():
117-
data = get_input("octal")
118-
decimal, binary, octal, hexadecimal = convertion(3, data)
119-
result(decimal, binary, octal, hexadecimal)
120-
confirmation("octal")
121-
122-
def hexadecimal():
123-
data = get_input("hexadecimal")
124-
decimal, binary, octal, hexadecimal = convertion(4, data)
125-
result(decimal, binary, octal, hexadecimal)
126-
confirmation("hexadecimal")
107+
confirmation(base)
127108

128109
# Logic
129110
def logic():
130-
select = input('Select your output : ')
131-
seperator()
132-
functions = {
133-
'1': decimal,
134-
'2': binary,
135-
'3': octal,
136-
'4': hexadecimal,
137-
'5': exit
138-
}
139-
function = functions.get(select, error)
140-
function()
141-
142-
# Init
143-
def init():
144111
menu()
145-
logic()
112+
select = input('Select your output : ')
113+
separator()
114+
if select == str(len(Base)+1):
115+
exit()
116+
elif select not in [str(base.value) for base in Base]:
117+
error("Invalid choice")
118+
else:
119+
main(Base(int(select)))
146120

147121
# Run
148-
init()
122+
logic()

0 commit comments

Comments
 (0)