#🔒 How to error handle an incomplete calculation expression?

13 messages · Page 1 of 1 (latest)

compact creek
#
    def to_calculate(self, expression):
        import re
        OPERATIONS = r"([-+/*x])"
        s = re.split(OPERATIONS, expression)
        print(f"{s=}")
        try:
            while len(s) > 2:
                result = self.calculate(s[1], s[0], s[2])
                temp = s[3:]
                temp.insert(0, result)
                s = temp
            return result
        except Exception as e:
            print(f"Something went wrong:\n{e}")

        
    def getfloat(self, number):
        try:
            num = float(number)
        except ValueError:
            print("Invalid number, try again")
            exit()
        return num

    def calculate(self, operator, number1, number2):
        number1 = self.getfloat(number1)
        number2 = self.getfloat(number2)

        if operator == "+":
            return self.add2numbers(number1, number2)
        if operator == "-":
            return self.subtract2numbers(number1, number2)
        if operator == "*" or operator == "x":
            return self.multiply2numbers(number1, number2)
        if operator == "/":
            return self.divide2numbers(number1, number2)
s=['12', '+', '3', '-', '']
Invalid number, try again
Future exception was never retrieved
future: <Future finished exception=SystemExit(None)>
Traceback (most recent call last):
  File "C:\Users\User\sides\python_flet\calculator\app.py", line 69, in getfloat
    num = float(number)
          ^^^^^^^^^^^^^
ValueError: could not convert string to float: ''

Because its incomplete, I don't know how to handle if the expression is 12+3-

high talonBOT
#

@compact creek

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

compact creek
#

So I checked a regular calculator on Windows 11, it seems to always start evaluating the expression as soon as the next math operation is entered. But that's not what I want

#

Is there another way of handling this incomplete math expression?

dry imp
#

if the expression is invalid, raise an exception
if you want to have error-handling code, catch that exception
that's what i'd do

#

i prefer the all-correct-or-nothing approach

compact creek
#

hmm

#

I think I'll just detect if it ends with a math operation, I'll remove the last operation and continue the calculation

#

yeah that works

#

!solve

#

!solved

high talonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.