Building the User Interface
Your conversion functions work — now users need a way to interact with them. For command-line programs, the user interface means clear prompts, organized menus, and readable output.
What Is a User Interface?
A user interface (UI) is how people interact with your program. For this project, it's a text-based CLI — users type commands and see text responses. The goal is making the experience intuitive, even without graphics.
Creating a Menu
A menu shows users their options. Here's a simple menu function:
def show_menu():
print("\n=== Number Base Converter ===")
print("1. Decimal to other bases")
print("2. Binary to decimal")
print("3. Octal to decimal")
print("4. Hex to decimal")
print("5. Quit")
return input("Choose an option (1-5): ")
The \n at the start adds a blank line for visual separation. The function displays options and returns the user's choice.
The Main Program Loop
Most interactive programs use a loop that keeps running until the user decides to quit:
while True:
choice = show_menu()
if choice == "5":
print("Thanks for using the converter!")
break
elif choice == "1":
num = int(input("Enter a decimal number: "))
print(f"Binary: {decimal_to_binary(num)}")
print(f"Octal: {decimal_to_octal(num)}")
print(f"Hex: {decimal_to_hex(num)}")
elif choice == "2":
binary = input("Enter a binary number: ")
print(f"Decimal: {binary_to_decimal(binary)}")
elif choice == "3":
octal = input("Enter an octal number: ")
print(f"Decimal: {octal_to_decimal(octal)}")
elif choice == "4":
hex_num = input("Enter a hex number: ")
print(f"Decimal: {hex_to_decimal(hex_num)}")
else:
print("Invalid choice. Please enter 1-5.")
The while True creates an infinite loop. The break statement exits when users choose option 5.
Formatting Output Clearly
Notice the f-strings like f"Binary: {decimal_to_binary(num)}". These format strings make output readable by labeling each result. Users see:
Binary: 101010
Octal: 52
Hex: 2A
Instead of just numbers without context.
Handling Unknown Choices
The else clause catches invalid menu choices. Without it, entering "7" would do nothing — confusing for users. Always tell users when something goes wrong.
Testing the Interface
Run your program and try each menu option. Does the flow feel natural? Are prompts clear? Can you easily convert numbers back and forth?
This is a good time to commit your changes to Git. You have working code worth saving.
What's Missing
Right now, entering "abc" when asked for a decimal number crashes the program. The next lesson addresses this with input validation — making your converter robust against unexpected input.