Noob questions about Python

Michele Simionato michele.simionato at gmail.com
Fri Oct 19 00:43:59 EDT 2007


On Oct 18, 7:44 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
> It returns '' when number == 0, so you need to test for that case:
>
> > def baseN(number, N=2):
> >     """
> >     >>> baseN(9, 2)
> >     '1001'
> >     """
> >     assert 2 <= N <= 10
> >     assert isinstance(number, int) and number >= 0
>
>     if number == 0:
>         return "0"
>
> >     b = []
> >     while number:
> >         b.append(str(number % N))
> >         number /= N
> >     return ''.join(reversed(b))

Right, or you can just change the last line:

    return ''.join(reversed(b)) or '0'


 Michele Simionato




More information about the Python-list mailing list