Last active
January 30, 2026 04:27
-
-
Save elyssonmr/ad6995b62271768d1411c4571e0f9957 to your computer and use it in GitHub Desktop.
Gist for the article: https://blog.elyssonmr.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Calculator: | |
| def __init__(self): | |
| self._history = [] | |
| @property | |
| def history(self): | |
| return self._history | |
| def _add_history_entry( | |
| self, num1: int, num2: int, operation: str, result: int | |
| ): | |
| self._history.append({ | |
| 'num1': num1, | |
| 'num2': num2, | |
| 'operation': operation, | |
| 'result': result | |
| }) | |
| def sum_numbers(self, num1: int, num2: int): | |
| result = num1 + num2 | |
| self._add_history_entry(num1, num2, '+', result) | |
| return result | |
| calculator = Calculator() | |
| exit = False | |
| while not exit: | |
| print('Operation Sum') | |
| num1 = int(input('Operador 1: ')) | |
| num2 = int(input('Operador 2: ')) | |
| if num1 != 0 and num2 != 0: | |
| print(f'Resultado {calculator.sum_numbers(num1, num2)}\n') | |
| else: | |
| print('\nOperations history\n') | |
| for entry in calculator.history: | |
| num1 = entry['num1'] | |
| operation = entry['operation'] | |
| num2 = entry['num2'] | |
| result = entry['result'] | |
| print(f'{num1} {operation} {num2} = {result}') | |
| exit = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Calculator: | |
| def __init__(self): | |
| self._history = [] | |
| @property | |
| def history(self): | |
| return self._history | |
| def _add_history_entry( | |
| self, num1: int, num2: int, operation: str, result: int | |
| ): | |
| self._history.append({ | |
| 'num1': num1, | |
| 'num2': num2, | |
| 'operation': operation, | |
| 'result': result | |
| }) | |
| def SumNumbers(self, num1: int, num2: int): | |
| result = num1 + num2 | |
| self._add_history_entry(num1, num2, '+', result) | |
| return result | |
| calculator = Calculator() | |
| exit = False | |
| while not exit: | |
| print('Operation Sum') | |
| num1 = int(input('Operador 1: ')) | |
| num2 = int(input('Operador 2: ')) | |
| if num1 != 0 and num2 != 0: | |
| print(f'Resultado {calculator.SumNumbers(num1, num2)}\n') | |
| else: | |
| print('\nOperations history\n') | |
| for entry in calculator.history: | |
| num1 = entry['num1'] | |
| operation = entry['operation'] | |
| num2 = entry['num2'] | |
| result = entry['result'] | |
| print(f'{num1} {operation} {num2} = {result}') | |
| exit = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment