Python Forum

Full Version: Binary to decimal, print the decimal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Trying to print the decimal number after each row, no success. Is it correct to use a variable (decimal) and then print it?

import random

def outputConverted(binary):

	decimal = int(bin,2)

	print(decimal)
    

for i in range(0,4):

    binary = ""

    for j in range(0,4):

        binary += str(random.randint(0, 1))

    print ('Binary  ' + binary +  ' in decimal: ')

outputConverted(binary)
You might take a look a 2 things:
1) your def outputConverted receives 1 argument, with a name that is not used.
2) the indentation of line 20

Paul
(Jul-11-2020, 09:41 AM)DPaul Wrote: [ -> ]You might take a look a 2 things:
1) your def outputConverted receives 1 argument, with a name that is not used.
2) the indentation of line 20

Paul

Thanks, the indentation was wrong so I changed it. Now I get 4 rows with the text Binary 0100 in decimal: and under the last row one random number is seen. The point is to pass a random generated number to the function and then to print it after every row... is the variable decimal wrong?
Did you correct my point # 1 ?

Paul