如题。就是照着教材上的代码敲的,实在是看不出来哪里不对,救救孩子::>_<::
from pythonds.basic.stack import Stack # As previously defined
r_stack = Stack()
def to_str(n, base):
convert_string = "0123456789ABCDEF"
while n > 0:
if n < base:
r_stack.push(convert_string[n])
else:
r_stack.push(convert_string[n % base])
n = n // base
res = ""
while not r_stack.is_empty():
res = res + str(r_stack.pop())
return res
print(to_str(1489, 16))
AttributeError Traceback (most recent call last)
<ipython-input-17-00efae08601c> in <module>
13 res = res + str(r_stack.pop())
14 return res
---> 15 print(to_str(1453, 16))
<ipython-input-17-00efae08601c> in to_str(n, base)
10 n = n // base
11 res = ""
---> 12 while not r_stack.is_empty():
13 res = res + str(r_stack.pop())
14 return res
AttributeError: 'Stack' object has no attribute 'is_empty'