[Tutor] Decimal Conversion

Alan Gauld alan.gauld at btinternet.com
Mon Oct 15 23:48:21 CEST 2007


<ddm2 at sfu.ca> wrote

> print ""
> print "--------------------"
> print "Unsigned Binary"
> print "--------------------"
> print ""

You don;t need the empty quotes to print a newline.

In fact the whole of the above is easier done using triple quotes

print """
--------------------
Unsigned Binary
--------------------
"""

It has nothing to do with your problem but for future reference...

To the real issue:

> n = int(raw_input("Please enter an integer: "))
> b = ''

so n is an integer and b is a string.

> while n > 0:
>    r = n%2
>    n = n/2
>    b = r,b

Michael has already explained that this is building 
a tuple of tuples. But you want to create strings.
So first convert r to a string using str(r)
Now you can append using the plus operation for strings

b = r+b

or just 

b += r

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list