[Python] How to split parenthesis in order to do priority of arithemtic operations in a python...

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently trying to create a fully functional calculator using PyQt6. The basic operations are working. My next goal for my app to be able to solve arithmetic operations with numbers in between parenthesis. I am really stuck on how i am supposed to:

    1. Split the parenthesis and
    2. Make it functional with the rest of the code

    Another problem i have is how to handle more than 2 numbers for operations.

    Here is the code:

    import sys
    from PyQt6.QtWidgets import *

    app = QApplication(sys.argv)
    window = QWidget()
    window.setWindowTitle("Calculator")
    window.resize(354, 550)
    window.move(465, 45)
    window.setStyleSheet("background-color: lightsteelblue;")
    window.show()

    result_display = QLineEdit(window)
    result_display.resize(340, 100)
    result_display.setStyleSheet("font-size: 24px;")
    result_display.setReadOnly(True)
    result_display.show()

    grid_layout = QGridLayout()
    grid_layout.addWidget(result_display, 0, 0, 1, 4)
    button_width = 80
    button_height = 80

    buttons_info = [("C", 1, 0), ("⌫", 1, 1), ("^", 1, 2), ("÷", 1, 3),
    ("7", 2, 0), ("8", 2, 1), ("9", 2, 2), ("×", 2, 3),
    ("4", 3, 0), ("5", 3, 1), ("6", 3, 2), ("-", 3, 3),
    ("1", 4, 0), ("2", 4, 1), ("3", 4, 2), ("+", 4, 3),
    ("()",5, 0), ("0", 5, 1), (".", 5, 2), ("=", 5, 3)]

    first_number = None
    operation = None
    current_value = 0

    def button_clicked(text):
    global first_number, operation
    current_text = result_display.text()

    if text == 'C':
    result_display.setText('')
    first_number = None
    operation = None
    elif text == '⌫':
    result_display.setText(current_text[:-1])
    elif text in ['+', '-', '×', '÷', "^", "()"]:
    if current_text != '':
    if first_number is None:
    first_number = float(current_text)
    operation = text
    result_display.setText(current_text + text)
    elif text == '=':
    if current_text != '' and first_number is not None:
    second_number = float(current_text.split(operation)[-1])
    if operation == '+':
    result_value = first_number + second_number
    elif operation == '-':
    result_value = first_number - second_number
    elif operation == '×':
    result_value = first_number * second_number
    elif operation == '÷':
    if second_number == 0:
    result_value = "Error"
    else:
    result_value = round(first_number / second_number, 3)
    elif operation == "^":
    result_value = first_number ** second_number
    result_display.setText(str(result_value))
    first_number = None
    operation = None
    elif operation == "()":
    pass
    else:
    result_display.setText(current_text + text)

    for name, row, col in buttons_info:
    button = QPushButton(name)
    button.setFixedSize(button_width, button_height)
    button.setStyleSheet("background-color: cornflowerblue;")
    button.clicked.connect(lambda checked, text=name: button_clicked(text))
    grid_layout.addWidget(button, row, col)

    window.setLayout(grid_layout)
    sys.exit(app.exec())


    I am a beginner so my code is probably not that good and i know that a lot of things i could have written them better. Also english is not my first language so i don't know whether i have explained my problem well.

    Any other tips are welcome :)

    Continue reading...

Compartilhe esta Página