2016-01-02 49 views
-4

我想嘗試在Python中使用堆棧將十進制數轉換爲二進制數,但是出現此錯誤。如何在Python中將NoneType轉換爲int?

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)) 
+5

什麼錯誤?哪裏? – Daniel

+0

Python列表也是一個堆棧。爲什麼要單獨上課? – Daniel

+5

@Daniel - 因爲真正的程序員可以用任何語言編寫Java。 – TigerhawkT3

回答

5

您的pop方法缺少return