A java hobbyist programmer learning python

elhombre elhmbre at ozemail.com.au
Sun Jan 18 02:11:45 CET 2009


Hello, below is my first fragment of working python code. As you can see it 
is very java like as that is all I know. Is this the right approach to be 
taking?
Should I be taking a different approach? Thanks in advance.

import sys

class Calculator():

    def __init__(self):
        self.operator = sys.argv[1]
        self.arg1 = sys.argv[2]
        self.arg2 = sys.argv[3]

    def getOperator(self):
        return sys.argv[1]

    def getArg1(self):
        return sys.argv[2]

    def getArg2(self):
        return sys.argv[3]

    def calculate(self):
        if self.getOperator() == '+' :
            return int(self.getArg1()) + int(self.getArg2())
        elif self.getOperator() == '*' :
            return int(self.getArg1()) * int(self.getArg2())
        elif self.getOperator() == '/' :
            return int(self.getArg1()) / int(self.getArg2())
        elif self.getOperator() == '-' :
            return int(self.getArg1()) - int(self.getArg2())
        else:
            return 'Wrong argument supplied'

x = Calculator()
y = x.calculate()
print y 




More information about the Python-list mailing list