Calculadora: configurando a potenciação com math.pow

This commit is contained in:
Luiz Otávio
2023-03-03 08:21:57 -03:00
parent b379fae718
commit 1b028cb86f

View File

@@ -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