Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Need help with classes/inheritance

Reply
Thread Tools

Need help with classes/inheritance

 
 
maxiaojun maxiaojun is offline
Junior Member
Join Date: Feb 2011
Posts: 1
 
      02-07-2011
This is the error I am getting:
Traceback (most recent call last):
File "C:\Users\Ma\Desktop\CS\cs173\calc.py", line 19, in <module>
main()
File "C:\Users\Ma\Desktop\CS\cs173\calc.py", line 17, in main
print(calc.top())
File "C:\Users\Ma\Desktop\CS\cs173\RPN.py", line 36, in top
return self._calcStack.top()
AttributeError: Stack instance has no attribute '__len__'

this is my code:
import RPN

def main():
calc = RPN.RPNCalculator()
RPNexpression = str(input("Enter RPN expression in string form and spaces between each number/operand: "))
print(RPNexpression)
RPNList = RPNexpression.split()
print(RPNList)
for i in RPNList:
calc.push(i)
print(calc.top())

import stack

class RPNCalculator(stack.Stack):

def __init__(self):
self._calcStack = stack.Stack()

def push(self, operand):
if operand == '+':
self.add()
elif operand == '-':
self.subtract()
elif operand == '*':
self.multiply()
elif operand == '/':
self.divide()
else:
self._calcStack.push(int(operand))

def top(self):
return self._calcStack.top()

def add(self):
try:
num2 = self._calcStack.pop()
num1 = self._calcStack.pop()
newSum = num1 + num2
self._calcStack.push(newSum)
except RuntimeError:
pass

def subtract(self):
try:
num2 = self._calcStack.pop()
num1 = self._calcStack.pop()
newSum = num1 - num2
self._calcStack.push(newSum)
except RuntimeError:
pass

def multiply(self):
try:
num2 = self._calcStack.pop()
num1 = self._calcStack.pop()
newSum = num1 * num2
self._calcStack.push(newSum)
except RuntimeError:
pass

def divide(self):
if self._calcStack.top() == 0:
raise ArithmeticError
try:
num2 = self._calcStack.pop()
num1 = self._calcStack.pop()
newSum = num1 / num2
self._calcStack.push(newSum)
except RuntimeError:
pass


class Stack:

def __init__(self):
self._stack = []

def top(self):
return self._stack[len(self._stack)-1]

def push(self, item):
self._stack.append(item)

def pop(self):
return self._stack.pop()

def isEmpty(self):
if len(self._stack) != 0:
return False
return True

def __len__(self):
return len(self._stack)

These are all separate python files but i imported them correctly. I really am at a loss because the error seems to have nothing to do with the code the error is pointing to. Sorry about indenting I don't know how to preserve it in forum posts but I can 100% guarantee you indenting is correct.
 

Last edited by maxiaojun; 02-07-2011 at 12:42 AM..
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Help Help. I really need some help with this =?Utf-8?B?Q2hyaXM=?= ASP .Net 3 01-31-2007 09:33 PM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM
Need help! I need to add lead zeros to a textbox Teep ASP .Net 2 06-21-2004 01:04 PM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM



Advertisments