From 1b028cb86f2a68814b5cb204a34512821299d5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Fri, 3 Mar 2023 08:21:57 -0300 Subject: [PATCH] =?UTF-8?q?Calculadora:=20configurando=20a=20potencia?= =?UTF-8?q?=C3=A7=C3=A3o=20com=20math.pow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aula202-calculadora/buttons.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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