[Tutor] Binary to Decimal conversion

Chris Castillo ctcast at gmail.com
Tue Mar 10 16:51:49 CET 2009


Thanks for clearing that up. I knew it was much simpler than I was trying to
make it I just couldn't quite see the logic that I needed for the problem
clearly. Thanks for the elegant code.

On Mon, Mar 9, 2009 at 10:53 PM, Moos Heintzen <iwasroot at gmail.com> wrote:

> You're making it more complicated than it needs to.
> Also, you first used binnum then binum, and you didn't define binsum.
>
> It could easily be done like this:
>
> binnum = raw_input("Please enter a binary number:  ")
> decnum = 0
> rank = 1
>
> for i in reversed(binnum):
>    decnum += rank * int(i)
>    rank *= 2
>
> Moos
> _______________________________________________
> 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/20090310/5f4956e2/attachment-0001.htm>


More information about the Tutor mailing list