r/PythonLearning • u/LovelyEbaa • Jul 16 '25
Help Request Could it be simpler ?
I'm totally new to programming in general not only in Python so as according to the advises I received, many people told me to code instead of watching tutorials only and so I did, I made this simple calculator with instructions of course but could it be easier and simpler than this ?
178
Upvotes
2
u/Long-Reveal-1493 Jul 17 '25 edited Jul 17 '25
1.) Do not repeat your code. Your print statement for the first number and second number repeats 4 times. You can put those two before the if statement.
2.) your if-statement works. but you can have a different approach
This is not a good approach but you can do this:
This code works by checking if the user inputs 1 or ADD or 2 or Substract.
ormeans if the input is either "1" or "ADD", the if-statement is truewe use
.upper()in this case so even if the input is lowercase, it would still work.The better approach is to use a dictionary.
The dictionary in this case is
actions[](). It is also like an if-statement but more readable way to use.inchecks if the operation is inside the actions, if yes call the actions.lambdais an anonymous functionif you learnt how to use functions, you can change the answer to functions.