我想尝试使用Python中的堆栈将十进制数转换为二进制数,但是我得到了这个错误.
class Stack: def __init__(self): self.items = [] def is_empty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): return len(self.items) def divideBy2(decNumber): remstack = Stack() while decNumber > 0: rem = decNumber % 2 remstack.push(rem) decNumber = decNumber // 2 binString = "" while not remstack.is_empty(): binString = binString + str(remstack.pop()) return binString print(divideBy2(42))
Daniel.. 5
你的pop
方法遗漏了return
.
你的pop
方法遗漏了return
.