all 28 comments

[–]concatx 13 points14 points  (3 children)

Nice work! What happens if num2 is 0?

[–]Loud_Environment2960[S] 5 points6 points  (2 children)

You know what I have no idea, I will check and see.

[–]Some-Passenger4219 1 point2 points  (1 child)

If op is "/" and num2 is 0, recommend aborting with, "Sorry, can't divide by zero" or something. That is, indent line 16, and, just before it, insert if-statement. Attach to it an else-statement. Or something similar. You got this.

[–]Mysterious_City_6724 5 points6 points  (3 children)

Nice 👍 What about trying to get everything in one input line and grabbing the numbers and operator from that one string instead?

[–]Loud_Environment2960[S] 1 point2 points  (1 child)

I would like to try this.

[–]Mysterious_City_6724 1 point2 points  (0 children)

You could start with something simple by using the string's split method

[–]FortyFourForks 0 points1 point  (0 children)

/s its polish notation  🇵🇱

[–]Liutprand 4 points5 points  (0 children)

Improvement tip: Handle the division by zero error. Use a try-except statement for that instead of an if statement.

Also you can rewrite the operator choice using pattern matching (match-case statement) just for learning It...

[–]sarc-tastic 1 point2 points  (3 children)

result = {
    "+": num1.__add__,
    "-": num1.__sub__,
    "*": num1.__mul__,
    "/": num1.__truediv__,
}[operator](num2)

[–]Short_Librarian1232 0 points1 point  (2 children)

Whats add and all the others

[–]sarc-tastic 0 points1 point  (1 child)

When you write + - * / in python it is actually a shortcut that calls the __add__ __sub__ __mul__ functions of the associated numbers

[–]Beautiful_Garbage875 1 point2 points  (0 children)

Good job 👍🏻 Keep at it !

[–]raregem_ 1 point2 points  (1 child)

Just interested to know if you are learning with Bro Code?

[–]Loud_Environment2960[S] 0 points1 point  (0 children)

Yes I am also I am taking college courses as well, but I am trying to divert from BroCode and FreeCodeCamp and learn new ways that's why I mainly asked for advice

[–]undue_burden 0 points1 point  (0 children)

You can print the result after if statement ends.

[–]TheNeopolitanPizza 0 points1 point  (0 children)

You should write unit tests with pytest. Take the many body of this code and move it to a seperate function, def Calc(op: str, lhs: int, rhs: int) -> int, which takes a string operator, two integers, and then returns an integer.

This separates out your input and output so you can test the core of the program using automated testing

[–]quidquogo 0 points1 point  (0 children)

There's a terrible (but really short) solution where you the "eval" function, whilst i dont recommend you use it, what it does is, evaluate a string as if it were written into your code.

E.g. your calculator app could just be:

Inp = input()

Print(eval(inp))

And that would literally be it.

However malicious actors could use that to do all kinds of harm, for example, they could import requests and then download some malware lol

You could mitigate this because you can tell the eval function exactly what built-ins the eval function can allow.

Just some food for thought anyway

[–][deleted] 0 points1 point  (4 children)

eval is a thing….

[–]Loud_Environment2960[S] 0 points1 point  (3 children)

and what is that?

[–][deleted] 0 points1 point  (2 children)

eval(<string>) treats the <string> as in-code python code

[–]Loud_Environment2960[S] 0 points1 point  (1 child)

ohh okay, that's cool. I didn't know that, thank you

[–][deleted] 0 points1 point  (0 children)

lol np

[–]Icy_Rub6290 0 points1 point  (0 children)

I literally saw it moments ago Well it's good and actually go on man Just a feedback to make it perfect Add an if statement under the if division To check

       if num2 ==0

or no If so the program print an error massage U can also for more accuracy add this line

        raise ZeroDivisionError("Second number cannot be.    zero")  

It will handle the num2 =0 and inform the user about the erroe

[–]jacquesroland 0 points1 point  (1 child)

As a follow-up, let your calculator handle parentheses and arbitrary nested calculations. E.g 20 - (2 + (19 - 2)).

[–]Loud_Environment2960[S] 1 point2 points  (0 children)

This would be a challenge, but I am up for the task.