I’m so so happy right now…
I just finished completing the ‘arithmetic calculator’ project in freecodecamp and I’m just feeling a sense of accomplishment right now
Special thanks to @kevinSmith and @Jagaya for making this a success…
For a start, if you got if-else, but nothing in one block, consider changing the condition so you only have an if-block.
#instead of
if(x>5):
pass
else:
#some code
#do
if (x<=5):
#some code
Then consider using max() to get the longer number, instead of writing two cases depending on which number is longer.
On top of that the operator only changes if you add or subtract the two numbers, so you should only change the calculation of the result → right now you have almost identical code twice.
On top of that if(-)-elif(+)-else(pass) is redundant. You already checked that the operator is either + or -. That means you only need if(+)-else, because if it isn’t +, it has to be -.
Finally, on the note of repeated code: given the thing you return is definitly 3 lines and the boolean only determines if the 4th line is added, here you could just combine the 3 lines and then have an if() to maybe add the 4th line and then return the result.
This makes it more obvious for a reader that the boolean only changes the last line and nothing else.