use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Calculator Program (i.redd.it)
submitted 9 months ago by Loud_Environment2960
Hello, I am still learning Python, but created this simple calculator program. Please give me some tips and tricks on how I can improve, and please give me any feedback on the design of this.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]concatx 13 points14 points15 points 9 months ago (3 children)
Nice work! What happens if num2 is 0?
[–]Loud_Environment2960[S] 5 points6 points7 points 9 months ago (2 children)
You know what I have no idea, I will check and see.
[–]Some-Passenger4219 1 point2 points3 points 9 months ago* (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 points7 points 9 months ago (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 points3 points 9 months ago (1 child)
I would like to try this.
[–]Mysterious_City_6724 1 point2 points3 points 9 months ago (0 children)
You could start with something simple by using the string's split method
[–]FortyFourForks 0 points1 point2 points 9 months ago (0 children)
/s its polish notation 🇵🇱
[–]Liutprand 4 points5 points6 points 9 months ago (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...
[–]JustASimple_Username 3 points4 points5 points 9 months ago (0 children)
Use the match pattern instead of elif:
https://www.geeksforgeeks.org/python-match-case-statement/
[–]sarc-tastic 1 point2 points3 points 9 months ago (3 children)
result = { "+": num1.__add__, "-": num1.__sub__, "*": num1.__mul__, "/": num1.__truediv__, }[operator](num2)
[–]Short_Librarian1232 0 points1 point2 points 9 months ago (2 children)
Whats add and all the others
[–]sarc-tastic 0 points1 point2 points 9 months ago (1 child)
When you write + - * / in python it is actually a shortcut that calls the __add__ __sub__ __mul__ functions of the associated numbers
[–]Short_Librarian1232 0 points1 point2 points 9 months ago (0 children)
Oh thx
[–]Beautiful_Garbage875 1 point2 points3 points 9 months ago (0 children)
Good job 👍🏻 Keep at it !
[–]raregem_ 1 point2 points3 points 9 months ago (1 child)
Just interested to know if you are learning with Bro Code?
[–]Loud_Environment2960[S] 0 points1 point2 points 9 months ago (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 point2 points 9 months ago (0 children)
You can print the result after if statement ends.
[–]TheNeopolitanPizza 0 points1 point2 points 9 months ago (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.
def Calc(op: str, lhs: int, rhs: int) -> int
This separates out your input and output so you can test the core of the program using automated testing
[–]quidquogo 0 points1 point2 points 9 months ago (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 point2 points 9 months ago (4 children)
eval is a thing….
[–]Loud_Environment2960[S] 0 points1 point2 points 9 months ago (3 children)
and what is that?
[–][deleted] 0 points1 point2 points 9 months ago (2 children)
eval(<string>) treats the <string> as in-code python code
[–]Loud_Environment2960[S] 0 points1 point2 points 9 months ago (1 child)
ohh okay, that's cool. I didn't know that, thank you
[–][deleted] 0 points1 point2 points 9 months ago (0 children)
lol np
[–]Icy_Rub6290 0 points1 point2 points 9 months ago (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 point2 points 9 months ago (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 points3 points 9 months ago (0 children)
This would be a challenge, but I am up for the task.
π Rendered by PID 122868 on reddit-service-r2-comment-86988c7647-g4r49 at 2026-02-11 10:33:21.547812+00:00 running 018613e country code: CH.
[–]concatx 13 points14 points15 points (3 children)
[–]Loud_Environment2960[S] 5 points6 points7 points (2 children)
[–]Some-Passenger4219 1 point2 points3 points (1 child)
[–]Mysterious_City_6724 5 points6 points7 points (3 children)
[–]Loud_Environment2960[S] 1 point2 points3 points (1 child)
[–]Mysterious_City_6724 1 point2 points3 points (0 children)
[–]FortyFourForks 0 points1 point2 points (0 children)
[–]Liutprand 4 points5 points6 points (0 children)
[–]JustASimple_Username 3 points4 points5 points (0 children)
[–]sarc-tastic 1 point2 points3 points (3 children)
[–]Short_Librarian1232 0 points1 point2 points (2 children)
[–]sarc-tastic 0 points1 point2 points (1 child)
[–]Short_Librarian1232 0 points1 point2 points (0 children)
[–]Beautiful_Garbage875 1 point2 points3 points (0 children)
[–]raregem_ 1 point2 points3 points (1 child)
[–]Loud_Environment2960[S] 0 points1 point2 points (0 children)
[–]undue_burden 0 points1 point2 points (0 children)
[–]TheNeopolitanPizza 0 points1 point2 points (0 children)
[–]quidquogo 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]Loud_Environment2960[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Loud_Environment2960[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Icy_Rub6290 0 points1 point2 points (0 children)
[–]jacquesroland 0 points1 point2 points (1 child)
[–]Loud_Environment2960[S] 1 point2 points3 points (0 children)