:mrgreen: hi im am tryingto create a dimensional analysis thing going on and so far i have this

def mult(self,other):
        tempTuple1=list()
        tempTuple2=list()
       
        i=0
        for unit,value in other.expr:
            for unit2,value2 in self.expr:
                if unit2 == unit:
                    tempTuple1.append((unit, value+value2))
                    #self.expr[i]=tempTuple1
                    
                   
##        for unit2,value2 in self.expr:
##            for unit,value in other.expr:
##                if unit2 != unit:
##                   tempTuple1.append((unit2,value2))


        print tempTuple1

and my output is so far like this:
>>> t = dimensions([('j',2),('km',1), ('f',5)])
>>> w = dimensions([('j',2),('f',1)])
>>> w.mult(t)
[('j', 4), ('f', 6)] need to append km1.... what am i doing wrong anyone? thanks for your help

class dimensions:
def __init__(self, listofTuples):
self.expr = listofTuples
 
def sub(self,other):
for value in self.expr:
if not value in other.expr:
return "incompatible"

return self.expr
def mult(self,other):
tempTuple1=list()
tempTuple2=list()

i=0
for unit,value in other.expr:
for unit2,value2 in self.expr:
if unit2 == unit:
tempTuple1.append((unit, value+value2))
tempTuple2.append(())

## for unit2,value2 in self.expr:
## for unit,value in other.expr:
## if unit2 != unit:
## tempTuple1.append((unit2,value2))
 
#print tempTuple1

here is my code now, i hope this is better

I don't know what editor you are using, but for some odd reason you lost all the indentations (critical for Python to work). I doctored up your code and added a number of temporary print statements, always good for troubleshooting. I noticed that your mult() has no return statement, so result = None is the returned.
Here is the doctored code:

class dimensions:
    def __init__(self, listofTuples):
        self.expr = listofTuples
 
    def sub(self, other):
        for value in self.expr:
            if not value in other.expr:
                return "incompatible"
            return self.expr

    def mult(self, other):
        tempTuple1=list()
        tempTuple2=list()
        print tempTuple1 # []
        print tempTuple2 # []
        print other.expr # [('j', 2), ('km', 1), ('f', 5)]
        print self.expr  # [('j', 2), ('f', 1)]

        i=0
        for unit,value in other.expr:
            for unit2,value2 in self.expr:
                if unit2 == unit:
                    tempTuple1.append((unit, value+value2))
                    tempTuple2.append(())

##      for unit2,value2 in self.expr:
##          for unit,value in other.expr:
##              if unit2 != unit:
##                  tempTuple1.append((unit2,value2))
 
        print tempTuple1 # [('j', 4), ('f', 6)]
        print tempTuple2 # [(), ()]
        # what does mult() return?????

t = dimensions([('j',2),('km',1), ('f',5)])
print t # <__main__.dimensions instance at 0x00AE6BE8>
w = dimensions([('j',2),('f',1)])
print w # <__main__.dimensions instance at 0x00AE6EE0>
print w.mult(t) # gives None, correct since there is no return

How new are you to Python?

sorry i just copied it and pasted it from my textedit. i was using the command line in gentoo.... i was working mostly with biopython and zope, never really persay did simple things in python. i just kinda skipped that and learned biopython syntax and the necessary python syntax to do my internship.

what i would like it to return is this: for example:
t = dimensions([('j',2),('km',1), ('f',5)])

w = dimensions([('j',2),('f',1)])

print w.mult(t) # gives None, correct since there is no return
return tempTuple1.append(the value that does not have a pair to be added with ('km',1)]

([('j',4),('f',6),('km',1)])

thanks for your help. what book would you recommend to learn python? i read some of the tutorials and they help but a book would be good.

To get a hang of the basics of Python, I would recommend the book "Learning Python" by Mark Lutz and David Ascher, published by O'Reilly (no relation to the buffoon on TV!)

still can't figure out how to append the last list into a tuple...does anyone stillhave any ideas. i tried popping and appending what was left and so forth. and yeah...still stuck any more ideas thanks!

This question has already been answered. Start a new discussion instead.