Python Forum
Binary to decimal, print the decimal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary to decimal, print the decimal
#1
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)
Reply
#2
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
(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?
Reply
#4
Did you correct my point # 1 ?

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert multiple decimal numbers in string to floats Katy8 6 3,570 Aug-16-2020, 06:06 PM
Last Post: Katy8
  Print Binary Tree - Python Daniel94 1 2,667 Jan-08-2020, 09:54 PM
Last Post: ichabod801
  Self Paced Web course - split ip and print each piece in binary and hex sumncguy 2 2,311 Dec-04-2019, 06:03 PM
Last Post: jefsummers
  binary to decimal program restingquarterback 3 1,978 Sep-18-2019, 06:24 AM
Last Post: DeaD_EyE
  converting decimal to binary missus_brown 1 2,147 Feb-19-2019, 02:42 AM
Last Post: stullis
  I can't figure out simple rounding to 2 decimal places! Funnypants 7 3,362 Nov-01-2018, 01:38 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020