Decimal to Binary Python Program
Below is a small code segment that takes a decimal number inputted by the user and converts it into binary.
decimal = int(input("Please enter a decimal value: \n"))
choice = int(input("What would you like to convert the decimal to? [1] Binary [2] Octal [3] Hexadecimal: \n"))
# if statements that convert the decimal into binary, octal, or hexadecimal depending on what number the user inputs
if choice == 1:
print("{x} in Binary is {y}".format(x=decimal,y=bin(decimal)[2:]))
elif choice == 2:
print("{x} in Octal is {y}".format(x=decimal,y=oct(decimal)))
elif choice == 3:
print("{x} in Hexadecimal is {y}".format(x=decimal,y=hex(decimal)))
#if the user tries to choose a number that is not one of the choice, this message will appear
else:
print("Input Invalid. Please try again.")
print("All Done!")