def on_input(self, w, event): text = sanitize(self.var.get()) self.var.set(text) try: val = mass(text) except ElementError as e: self.out.set(f"\"{e.elem}\" doesn't exist") self.output.config(bg="red") else: self.out.set(f"{val:.2f} g/mol") self.output.config(bg=self.cget("bg"))
def test_zero(self): self.assertEqual(mass(''), 0) self.assertEqual(mass('5'), 0)
def test_struct_dimethylpropane(self): struct = 'CH3(CH3)2CH3' self.assertEqual(mass(struct), 60.04) self.assertEqual(mass('5' + struct), 5 * 60.04)
def test_benzene(self): self.assertEqual(mass('C6H6'), 78.06) self.assertEqual(mass('5C6H6'), 5 * 78.06)
def test_nonelement(self): with self.assertRaises(ElementError): mass('Fee')
def test_elem(self): self.assertEqual(mass('H'), 1) self.assertEqual(mass('5H'), 5)