-1

I want a fully functioning calculator where you put in things like 1+2 or 1*2 or 1+a or 1*a, that sort of thing. However I am having some trouble when combining + and *. Any ideas how I could improve it?

What I have so far (sorry about the messy and unnecessary conversions from str to int and back):

class Math:
    def Calculate(stuff):
        result = 0
        resultstr = ""
        if "*" in stuff:
            y = stuff.split("*")
            for x in y:
                if result == 0:
                    result = x
                else:
                    try:
                        x = int(x)
                    except:
                        result = str(result) + x
                        result = result.replace("0*", "")
                        result = result.replace("*0", "")
                    else:
                        try:
                            result = int(result) * int(x)
                        except:
                            resultstr = str(result)
                            result = x
        if "+" in stuff:
            y = stuff.split("+")
            for x in y:
                try:
                    x = int(x)
                except:
                    result = str(result) + "+" + x
                    result = result.replace("0+", "")
                    result = result.replace("+0", "")
                else:
                    try:
                        result = int(result) + int(x)
                    except:
                        resultstr = str(result) + "+"
                        result = x
        
        finalresult = str(resultstr)+str(result)
        print(str(finalresult))
h = Math
h.Calculate("a*5+5")
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 2
    Welcome to Stack Overflow! Please take the [tour]. What do you mean by "having some trouble"? For debugging help, you need to make a [mre] including expected output and actual output -- or if you get an error, the [full error message with traceback](https://meta.stackoverflow.com/q/359146/4518341). You can [edit]. For more tips, see [ask]. – wjandrea Apr 04 '22 at 16:43
  • I skimmed the code and two things stuck out: 1) Why are you using a class for this? You never instantiate it and it doesn't seem to carry any state. (`h = Math` is just an alias, not an instantiation, if that's what you were thinking.) 2) [A bare `except` is bad practice](/q/54948548/4518341). Instead, use the specific exception you're expecting like `except ValueError`, or at least `except Exception`. – wjandrea Apr 04 '22 at 16:48

1 Answers1

0

Take the example you just gave (a*5+5). If I understand exactly your program, it will find that "stuff" contains *, so it will divide it into x=a and y=5+5. By doing so, you're assigning 5+5 to result which I don't think it's what you want to do.

PS: Little piece of advice: Try to name your variables in an understandable way. For example, I would call stuff something like query.

blackbrandt
  • 2,010
  • 1
  • 15
  • 32
Liuk23
  • 58
  • 7
  • If it helps, `stuff` is technically an *expression*, both in math terms and Python terms. `query` suggests that it's doing a lookup, which it isn't. – wjandrea Apr 04 '22 at 16:56
  • I see why query is not good, that was my bad (maybe ```expression```, as you said, is better?). Moreover, I didn't know ```stuff``` had another meaning outside ```things```. – Liuk23 Apr 04 '22 at 17:06
  • I'm referring to `stuff` as a variable, not a word. The string represents an expression. Sorry, I meant to write "query" in quotes, not code formatting, to differentiate it. – wjandrea Apr 04 '22 at 17:08
  • yeah, got to start naming stuff better. anyway with * it splits it either side like you said and it ends up ignoring order of operations and stuff, might have to re-write the equation forming bit. – Hagan Gray Apr 04 '22 at 18:42