Binary

Jerry Hill malaclypse2 at gmail.com
Fri Jun 8 15:51:49 EDT 2007


Brian, I'm including the list back in on this reply, so that more than
just me can see your message and potentially help out.

On 6/8/07, Brain Murphy <brianomorchoe at yahoo.co.uk> wrote:
>
> this is what i have so far.
> x= 100
> b=2
> while x > 0:
>     print x
>     x=divmod(x,b)
>
> the problem is that since it is a tuple I can not use that last part, x =
> divmod(x,b)
> how can I put this in a loop?
> were it goes through the whole number, as in 100/2=50 50/2 25 ect.
> but also keeping the remiander?

Since divmod() returns a tuple, you need to unpack it.  For example:
>>> q, r = divmod(10, 3)
>>> print q
3
>>> print r
1

If you want to store all the remainders as you go, maybe you could
build up a list of remainders by starting with an empty list and
append() the values to the list as you go through the loop.

-- 
Jerry



More information about the Python-list mailing list