Im going through the book Problem Solving with Algorithms and Data Structures for Python. Im in chapter 4 and am using the examples. This example is straight from the book, yet when I run it its nothing but errors. Is this a mistake in the book or am i missing something here?
def to_str(n, base):
convert_string = "0123456789ABCDEF"
if n < base:
return convert_string[n]
else:
return to_str(n / base, base) + convert_string[n % base]
print(to_str(1453, 16))
When I run this I get these errors:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in to_str
File "<stdin>", line 6, in to_str
File "<stdin>", line 4, in to_str
TypeError: string indices must be integers
Is the book wrong or am I missing something? If Its my fault, what could I be missing Ive reread this chapter in full twice now. Im not missing anything in the text.