diff --git a/aula202-calculadora/buttons.py b/aula202-calculadora/buttons.py index 447447d..aa2ed9e 100644 --- a/aula202-calculadora/buttons.py +++ b/aula202-calculadora/buttons.py @@ -1,3 +1,4 @@ +import math from typing import TYPE_CHECKING from PySide6.QtCore import Slot @@ -77,7 +78,7 @@ class ButtonsGrid(QGridLayout): if text == 'C': self._connectButtonClicked(button, self._clear) - if text in '+-/*': + if text in '+-/*^': self._connectButtonClicked( button, self._makeSlot(self._operatorClicked, button) @@ -136,14 +137,22 @@ class ButtonsGrid(QGridLayout): self._right = float(displayText) self.equation = f'{self._left} {self._op} {self._right}' - result = 0.0 + result = 'error' try: - result = eval(self.equation) + if '^' in self.equation and isinstance(self._left, float): + result = math.pow(self._left, self._right) + else: + result = eval(self.equation) except ZeroDivisionError: print('Zero Division Error') + except OverflowError: + print('NĂºmero muito grande') self.display.clear() self.info.setText(f'{self.equation} = {result}') self._left = result self._right = None + + if result == 'error': + self._left = None