You pass an integer to your function as a . Then you try to assign it as: a[k] = ... , but this does not work, since a is a scalar ...
This is the same thing you tried:
50[42] = 7
This statement does not make much sense, and the python will yell at you the same way (presumably).
Also, ++k does not do what you think it does - it is parsed as (+(+(k))) - that is, the byte code is simply UNARY_POSITIVE twice. What you really want is something like k += 1
Finally, be careful with statements such as:
q = q / b
The brackets that you use with printing mean that you want to use this in python3.x at some point. but x/y behaves differently on python3.x than on python2.x. Looking at the algorithm, I assume that you want integer division (since you check q != 0 , which is hard to satisfy with float). In this case, you should consider using:
q = q // b
which performs integer division on both python2.x and python3.x.
mgilson
source share