Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions Converter/converter.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from converter_values import * # import required files
from converter_values import options, CATEGORIES

def main():
print(options["help"]) # prints help menu
res = input("Response: ")

while res.lower() != "q": # program loop
try:
res = res.strip().split(" ")
tokens = res.strip().split(" ")

if len(res) == 1:
display_help(res[0]) # display help menu
elif len(res) == 4:
perform_conversion(res) # perform unit conversion
if len(tokens) == 1:
display_help(tokens[0]) # display help menu
elif len(tokens) == 4:
perform_conversion(tokens) # perform unit conversion
else:
print("Invalid command")
print("Invalid command. Format: <Category> <Unit> <Value> <TargetUnits>")

except Exception as e:
print("Error:", e)
Expand All @@ -22,15 +22,44 @@ def main():

def display_help(command):
"""Display help menu."""
print(options[command])
if command in options:
print(options[command])
else:
print(f"Unknown command '{command}'. Type 'help' or 'symbols'.")

def perform_conversion(res):
"""Perform unit conversion."""
for i in res[3].split(','):
value = round(eval("{} * {}['{}'] / {}['{}']".format(res[2], res[0], i, res[0], res[1])), 6) # calculating
print("{} \t : {}".format(i, value)) # displaying
"""Perform unit conversion cleanly without unsafe eval."""
category_code = res[0].upper()
src_unit = res[1]

if category_code not in CATEGORIES:
print(f"Invalid category '{res[0]}'. Valid categories: {', '.join(CATEGORIES.keys())}")
return

unit_dict = CATEGORIES[category_code]

if src_unit not in unit_dict:
print(f"Invalid source unit '{src_unit}' for category '{category_code}'.")
return

try:
raw_val = float(res[2])
except ValueError:
print(f"Invalid numeric value '{res[2]}'.")
return

target_units = [u.strip() for u in res[3].split(',')]
for target in target_units:
if target not in unit_dict:
print(f"Invalid target unit '{target}' for category '{category_code}'.")
continue

# Calculation: val * unit_dict[target] / unit_dict[src_unit]
calc_value = round((raw_val * unit_dict[target]) / unit_dict[src_unit], 6)
print("{} \t : {}".format(target, calc_value))

if __name__ == "__main__":
main()



13 changes: 11 additions & 2 deletions Converter/converter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
inch : in
kilometer : km
meter : m
micrometer ; um
micrometer : um
mile : mi
millimeter : mm
nanometer : nm
Expand All @@ -21,7 +21,7 @@
sq inch : in2
sq km : km2
sq m : m2
sq mile : mi
sq mile : mi2
sq yard : yd2

VOLUME : V ----------------------------------------------------------------
Expand Down Expand Up @@ -115,3 +115,12 @@
"min":1440 ,
"sec":86400 }

CATEGORIES = {
"L": L,
"A": A,
"V": V,
"M": M,
"T": T
}


Binary file not shown.