// def decimal_to_binary(decimal) : integer_part = int(decimal) fractional_part = decimal - integer_part binary_integer = bin(integer_part)[2:] binary_fraction = '' while fractional_part: //fractional_part *= 2 //bit = int(fractional_part) //binary_fraction += str(bit) //fractional_part -= bit //if len(binary_fraction) > 10: # Prevent infinite loop for fractions //break //return f'{binary_integer}.{binary_fraction}' if binary_fraction else binary_integer //def binary_to_decimal(binary) : //integer_part, _, fractional_part = binary.partition('.') //decimal_integer = int(integer_part, 2) //decimal_fraction = sum(int(bit) * 2 * *-index for index, bit in enumerate(fractional_part, start = 1)) //return decimal_integer + decimal_fraction //def display_menu() : //print("Choose an option:") //print("1: Convert decimal to binary") //print("2: Convert binary to decimal") //return input("Enter 1 or 2: ") //def main() : //choice = display_menu() //if choice == '1' : //decimal = float(input("Enter a decimal number: ")) //print(f'The binary equivalent is: {decimal_to_binary(decimal)}') //elif choice == '2' : //binary = input("Enter a binary number: ") //print(f'The decimal equivalent is: {binary_to_decimal(binary)}') //else: //print("Invalid choice. Please enter 1 or 2.") //if _name_ == "_main_" : //main()
Standard input is empty
// def decimal_to_binary(decimal) : integer_part = int(decimal) fractional_part = decimal - integer_part binary_integer = bin(integer_part)[2:] binary_fraction = '' while fractional_part: //fractional_part *= 2 //bit = int(fractional_part) //binary_fraction += str(bit) //fractional_part -= bit //if len(binary_fraction) > 10: # Prevent infinite loop for fractions //break //return f'{binary_integer}.{binary_fraction}' if binary_fraction else binary_integer //def binary_to_decimal(binary) : //integer_part, _, fractional_part = binary.partition('.') //decimal_integer = int(integer_part, 2) //decimal_fraction = sum(int(bit) * 2 * *-index for index, bit in enumerate(fractional_part, start = 1)) //return decimal_integer + decimal_fraction //def display_menu() : //print("Choose an option:") //print("1: Convert decimal to binary") //print("2: Convert binary to decimal") //return input("Enter 1 or 2: ") //def main() : //choice = display_menu() //if choice == '1' : //decimal = float(input("Enter a decimal number: ")) //print(f'The binary equivalent is: {decimal_to_binary(decimal)}') //elif choice == '2' : //binary = input("Enter a binary number: ") //print(f'The decimal equivalent is: {binary_to_decimal(binary)}') //else: //print("Invalid choice. Please enter 1 or 2.") //if _name_ == "_main_" : //main()