Objective
Develop a Python program to prompt the user for a number and display it in both hexadecimal and binary. It must repeat until the user enters 0.
Example Python Exercise
Show Python Code
# Repeat until the user enters 0
while True:
# Prompt the user for a number
number = int(input("Enter a number (0 to stop): "))
# If the number is 0, exit the loop
if number == 0:
break
# Convert the number to hexadecimal and binary
hexadecimal = hex(number)
binary = bin(number)
# Display the results
print(f"Hexadecimal: {hexadecimal}")
print(f"Binary: {binary}")
print() # Blank line for better readability
Output
Enter a number (0 to stop): 10
Hexadecimal: 0xa
Binary: 0b1010
Enter a number (0 to stop): 255
Hexadecimal: 0xff
Binary: 0b11111111
Enter a number (0 to stop): 0
Share this Python Exercise
More Python Programming Exercises of Python Data Types
Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.
This Python program prompts the user to enter a decimal number and displays its equivalent in binary form. Instead of using the str() function, the prog...
This Python program uses the conditional operator to assign a boolean variable named "bothEven" the value "True" if both numbers entered by the user are...
This Python program prompts the user for a real number and displays its square root. The program uses a "try...except" block to handle any potential err...
This Python program prompts the user to input three letters and then displays them in reverse order. The program uses basic input and string manipulation techn...
This Python program prompts the user to input a symbol and a width, then displays a triangle of the specified width using that symbol for the inner part...
This Python program prompts the user for a username and a password (both should be strings) and repeats the process until the correct credentials are en...