However since r is an int and b is a string, you will get an error when you try and concatenate them. >>> b='' >>> b+=1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects
So you need to convert r to a string before you assign it to b >>> b="" >>> n=5 >>> r=str(n%2) >>> b=r+b >>> print b 1 >>> n=n/2 >>> r=str(n%2) >>> b=r+b >>> print b 01 >>> n=n/2 >>> r=str(n%2) >>> b=r+b >>> print b 101 Chris Henk [EMAIL PROTECTED] wrote on 10/15/2007 03:17:13 PM: > 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 < [EMAIL PROTECTED]> 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, [EMAIL PROTECTED] <[EMAIL PROTECTED]> 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@python.org > http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor