[Tutor] Decimal Conversion

Michael Langford mlangford.cs03 at gtalumni.org
Mon Oct 15 21:17:13 CEST 2007


After sending the last email, I was more and more unsatisfied with it's
level of detail I provided.

Your original statement:

b=b,r

was doing nothing like you intended. The comma operator in this instance is
making a tuple. The name b was being reassigned to the new tuple created by
the comma operator.

b+="," + r

Was not doing exactly what I said. What it's doing is creating a new string
from the one named by b, the string literal "," , and the one named by r.
After creating the string it assigns the name b to the new string.

      --michael

-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/

On 10/15/07, Michael Langford <mlangford.cs03 at gtalumni.org> wrote:
>
> Use
> b+=","+r
> instead. That will add the , to the string named by b, and will
> concatenate the string named by r to the end.
>
>    --Michael
>
> --
> Michael Langford
> Phone: 404-386-0495
> Consulting: http://www.TierOneDesign.com/
>
> On 10/15/07, ddm2 at sfu.ca <ddm2 at sfu.ca> wrote:
> >
> > Hi,
> >
> > I'm new to Python, and found some problems on the internet that would be
> > a
> > good start to begin. The problem I have is that my conversion program
> > (which
> > currently only converts unsigned integers) also prints all these
> > brackets
> > and commas. Here is my code and the result:
> > CODE:
> > print ""
> > print "--------------------"
> > print "Unsigned Binary"
> > print "--------------------"
> > print ""
> > n = int(raw_input("Please enter an integer: "))
> > b = ''
> > while n > 0:
> >     r = n%2
> >     n = n/2
> >     b = r,b
> > print b
> >
> > (ex: n = 15)
> > RESULT:
> > (1, (1, (1, (1, ''))))
> >
> > I think the problem is in the line of code 'b = r,b', but am not sure
> > how to
> > fix it. The tip given for the problem says I should 'append' r to b, but
> > since b is not a list, 'b = r,b' was the only thing that came to my
> > mind.
> >
> > Thanks in advance.
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071015/9306818e/attachment-0001.htm 


More information about the Tutor mailing list