Quantcast
Channel: Give a number units when used through a class - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by hallie for Give a number units when used through a class

$
0
0

I'm not sure if this is exactly what you're looking for, but you could have a method inside the Atom class that prints the mass of the atom plus a unit string.

class Atom:    def __init__(self, protons, electrons, neutrons):        ElemCharge = 1.6021765*(10**-19)        UMAS = 1.66054*(10**-27)        self.protons = protons        self.electrons = electrons        self.neutrons = neutrons        self.charge = ElemCharge*(self.protons - self.electrons)        self.mass = (self.protons + self.neutrons)*UMAS    def print_mass_str(self):      print('{} {}'.format(self.mass, 'kg'))

Then this:

Argon = Atom(18,18,22)print(Argon.mass)Argon.print_mass_str()

would print:

6.64216e-266.64216e-26 kg

Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>