OK, I'm building this reactor software (an assignment) that takes particles and shows a chain reaction.
Here is the code I have so far:
The problem is in the Reaction class. It should print out like this:
(6)Li + (2)H -> (4)He + (4)He
Right now its only giving me the following:
e- e+ p n nu_e gamma
(2)H
(6)li
(4)He
(((4)He, (4)He), <__main__.React ion instance at 0x00C42670>) (((4)He, (4)He), <__main__.React ion instance at 0x00C42670>)
<__main__.React ion instance at 0x00C42670>
>>>
The final one starting with (((4)He, (4)He), is the part I don't want and should look like the example I gave.
So, I think I'm going the right direction but its printing the second particles twice, and not using the first particles at all. Plus its giving me the address in memory, and I most definitely don't want that. At first I thought it was inheritance, but I can see now that it isn't. The Nucleus prints the correct format for one particle, so I need to reuse that to make this class print out what I need. Can someone give me a nudge in the right direction, please?
Thanks
Here is the code I have so far:
Code:
class Particle:
def __init__(self, symbol, charge, number):
self.symbol = symbol
self.charge = charge
self.number = number
def __repr__(self):
return self.symbol
class Nucleus(Particle):
def __repr__(self):
return "(%d)%s" % (self.number, self.symbol)
class UnbalancedCharge:
pass #worry about this later
class UnbalancedNumber:
pass #worry about this later
class Reaction: # this is the class I'm having trouble with
def __init__(symbol, charge, number):
lhs = number, symbol
rhs = number, symbol
print lhs # just a test
#def __repr__(lhs, rhs): //this is what I'll return later, when I get lhs and rhs to access correct information
#return lhs, rhs
if __name__ == '__main__':
em = Particle("e-", -1, 0)
ep = Particle("e+", 1, 0)
p = Particle("p", 1, 1)
n = Particle("n", 0, 1)
neutrino = Particle("nu_e", 0, 0)
gamma = Particle("gamma", 0, 0)
print em, ep, p, n, neutrino, gamma
d = Nucleus("H", 1, 2)
li6 = Nucleus("li", 3, 6)
he4 = Nucleus("He", 2, 4)
print d
print li6
print he4
print Reaction((li6, d), (he4, he4))
(6)Li + (2)H -> (4)He + (4)He
Right now its only giving me the following:
e- e+ p n nu_e gamma
(2)H
(6)li
(4)He
(((4)He, (4)He), <__main__.React ion instance at 0x00C42670>) (((4)He, (4)He), <__main__.React ion instance at 0x00C42670>)
<__main__.React ion instance at 0x00C42670>
>>>
The final one starting with (((4)He, (4)He), is the part I don't want and should look like the example I gave.
So, I think I'm going the right direction but its printing the second particles twice, and not using the first particles at all. Plus its giving me the address in memory, and I most definitely don't want that. At first I thought it was inheritance, but I can see now that it isn't. The Nucleus prints the correct format for one particle, so I need to reuse that to make this class print out what I need. Can someone give me a nudge in the right direction, please?
Thanks
Comment