I am a beginner in python. I have written a program to convert decimal numbers to binary. It works. But Is there any other way to make it better? Refer to my code. Thanks :)
def decimal_to_binary_converter(dec):
print(f"Decimal: {dec}")
binary = ""
# Using while loop dividing the decimal number by 2 and concatenating the reminder to the string variable "binary" .
# % Operator gets the reminder from the division.
while int(dec) > 0:
binary += str(int(dec)%2)
dec = int(dec) / 2
x = int(len(binary))
rev_binary = ""
# using this while loop reversing the string "binary" and showing the Output.
while x > 0:
rev_binary += str(binary[x-1])
x -= 1
return print(f"Binary: {rev_binary}")
decimal_to_binary_converter(945)
Output:
Decimal: 945
Binary: 1110110001