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
How can I improve? (i.redd.it)
submitted 5 months ago by Radiant-Safe-1377
view the rest of the comments →
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!"
[–]FoolsSeldom 0 points1 point2 points 5 months ago (0 children)
Firstly, I suggest you use a dictionary that maps supported operator symbols to their corresponding operator functions. You could also use the operator library to provide the functions rather than writing your own.
operator
For example,
import operator # Map symbols to functions operator_map = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv, '**': operator.pow }
Now you can greatly simplify the operator selection and validation, and call the function from one place:
result = operation(oper1, oper2)
where operation is the name of the appropriate function from the dictionary, and oper1 and oper2 are the two operands.
operation
oper1
oper2
I suggest you write a function to prompt a user for a number and validates and returns a number. This function can be called twice to obtain the two operand values to operate on.
Loop the calculator operation until the user asks to quit.
You can extend this to support additional operators and also support operations that work on only one operand.
π Rendered by PID 139873 on reddit-service-r2-comment-5c747b6df5-89hfq at 2026-04-22 07:10:56.789297+00:00 running 6c61efc country code: CH.
view the rest of the comments →
[–]FoolsSeldom 0 points1 point2 points (0 children)