Skip to content

Commit

Permalink
Merge pull request #12 from Sleepy4k/11-add-code-helper
Browse files Browse the repository at this point in the history
11 add code helper
  • Loading branch information
Sleepy4k authored Feb 24, 2023
2 parents 86f85e6 + 8d8f62d commit 92aae81
Showing 1 changed file with 61 additions and 50 deletions.
111 changes: 61 additions & 50 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,62 @@
# Import Modules
# Author: @sleepy4k
# License: MIT License
# Description: A simple number convertion application with Python.
import os
from enum import Enum

# Class
# Enumerate the base numbers.
class Base(Enum):
DECIMAL = 1
BINARY = 2
OCTAL = 3
HEXADECIMAL = 4
ASCII = 5

# Clear Command
# Clears the command prompt.
def clear_cmd():
os.system("cls") if os.name == "nt" else os.system("clear")

# separator
# Prints a separator line.
def separator():
print("-" * 30)

# Title
# Prints the application title.
def title():
clear_cmd()
separator()
print("Number Convertion with Python")
print("Simple Number Convertion with Python")
separator()

# Menu
# Prints the menu options.
def menu():
title()
for base in Base:
print(f"{base.value}. {base.name}")
print(f"{len(Base)+1}. Exit")
print(f"{len(Base) + 1}. Exit")
separator()

# Get Input
# Prompts the user for input data.
def get_input(base):
title()
data = input(f"Input your {base.name.lower()} number : ")
data = input(f"Input your {base.name.lower()} number or word : ")
if not data:
raise ValueError(f"{base.name.lower()} number is required")
return data

# Error
# Handles errors and prompts the user to continue or exit.
def error(err):
separator()
print(err)
print(f"System error : {err}")
separator()
choice = input("Do you want to continue? [yes/no] : ")
if choice.lower() in ["yes", "y"]:
logic()
return True
elif choice.lower() in ["no", "n"]:
exit()
logic()
else:
error("Invalid choice")
return error("Invalid choice")

# Convertion
# Converts the input data to decimal and other bases.
def convertion(base, data):
try:
match base:
Expand All @@ -67,31 +69,39 @@ def convertion(base, data):
case Base.HEXADECIMAL:
decimal = int(data, 16)
case Base.ASCII:
"""Convert string to array."""
decimal = [ord(character) for character in data]
binary = ''.join([bin(character)[2:] for character in decimal])
octal = ''.join([oct(character)[2:] for character in decimal])
hexadecimal = ''.join([hex(character)[2:] for character in decimal])

"""Convert array to other bases."""
binary = ' '.join([bin(character)[2:] for character in decimal])
octal = ' '.join([oct(character)[2:] for character in decimal])
hexadecimal = ' '.join([hex(character)[2:] for character in decimal])
asci = ''.join([chr(character) for character in decimal])

# Convert to string
decimal = ''.join([str(character) for character in decimal])
return (decimal, binary, octal, hexadecimal, asci)
case _:
error("Invalid base")
"""Convert array to decimal."""
decimal = ' '.join([str(character) for character in decimal])

"""Return the result."""
return [decimal, binary, octal, hexadecimal, asci]
"""Convert decimal to other bases."""
binary = bin(decimal)[2:]
octal = oct(decimal)[2:]
hexadecimal = hex(decimal)[2:]
asci = chr(decimal)
return (decimal, binary, octal, hexadecimal, asci)
except ValueError:
error("System has value error")
except TypeError:
error("System has data type error")

"""Return the result."""
return [decimal, binary, octal, hexadecimal, asci]
except ValueError as valErr:
error(valErr)
except TypeError as typeErr:
error(typeErr)
except Exception as err:
error(f"System error: {err}")
error(err)
"""Return the result."""
return main(base)

# Confirmation
def confirmation(base):
# Prompts the user to retry or exit.
def retry(base):
choice = input("Do you want to try again? [yes/no] : ")
separator()
if choice.lower() in ["yes", "y"]:
Expand All @@ -100,35 +110,36 @@ def confirmation(base):
logic()
else:
error("Invalid choice")
return retry(base)

# Result
def result(decimal, binary, octal, hexadecimal, asci):
separator()
print(f"Decimal : {decimal}")
print(f"Binary : {binary}")
print(f"Octal : {octal}")
print(f"Hexadecimal : {hexadecimal}")
print(f"ASCII : {asci}")
separator()

# Main
# Main logic.
def main(base):
data = get_input(base)
decimal, binary, octal, hexadecimal, asci = convertion(base, data)
result(decimal, binary, octal, hexadecimal, asci)
confirmation(base)
try:
data = get_input(base)
conversions = convertion(base, data)
separator()
print(f"Decimal : {conversions[0]}")
print(f"Binary : {conversions[1]}")
print(f"Octal : {conversions[2]}")
print(f"Hexadecimal : {conversions[3]}")
print(f"ASCII : {conversions[4]}")
separator()
retry(base)
except ValueError as err:
error(err)
main(base)

# Logic
# Initial Logic
def logic():
menu()
select = input('Select your output : ')
separator()
if select == str(len(Base)+1):
if select == str(len(Base) + 1):
exit()
elif select not in [str(base.value) for base in Base]:
error("Invalid choice")
else:
main(Base(int(select)))

# Run
# Run the application.
logic()

0 comments on commit 92aae81

Please sign in to comment.