Anirudha
← Work
15 Days of CodingDay 10September 19, 2025~5 min read

Functions with Outputs, Calculator program

Read the code before running when its directly suggested by the IDE

Okay, maybe I backtracked and did not go to bed early which resulted in very inadequate sleep. I am not to be blamed, its all on Zuckerberg for creating social media apps. Definitely not on me to stop scrolling and go to bed if there are reels playing continuously on loop.

Anyway, today I began by learning about functions with output and the difference between return and print statements inside a function. It gets really complex really fast if you start using recursive loops and return the output from a function and then use the output of that function inside another function. My only tip here is to understand why and what output you are asking from each function and map it out in a flowchart. Flowcharts are the easiest way to understand a code as well as to start defining and writing the same code. Step 1 should always be to sketch a flowchart. Once you understand the flow of a code, you can ace the coding using your functions and shorten it as needed.

Another tip would be to use docstrings(”””) to document your code. It is especially useful to write multiline comments to help others understand what you are trying to do. The best feature I found about docstrings is to use it just below the definition of a function and this automatically adds the information you have typed in the docstring to be displayed when you hover over your function call. Good way to remember why you used it after you forget your logic the next day.

For the project of the day, I created a simple calculator program wherein the user can calculate two different values they want. To do this, I first asked the user to give me 2 numbers as well as the operation they needed done on the numbers. Instead of programming a lot of lines with if, elif and else statements, I decided to store all the operations inside a dictionary with a key and used the corresponding values stored inside the dictionary to define my own functions.

Because of this, I could easily use a for loop on the dictionary and based on the operator selected by the user, call the value inside the corresponding key, and use this value to call the defined function and execute it based on the input values by the user. That was it. The main logic of my program and it worked well. No edge cases, no fault in the logic. But what if the user wanted to continue using the program using the calculated answer? For that, I used a while loop and asked the user if they wanted to continue. If they did, I overwrote the first number inside the logic with the calculated answer and asked the user for the second number. My logic so far was spot on but the result I was getting was wildly inaccurate.

After spending some time printing out each step of my logic, taking a short water break and trying to find out the flaw in my logic, I found the mistake. While writing my code to save the calculated answer, I had used the code suggested by the IDE and I used it after giving it a cursory glance and moved onto the next line. But, the calculated answer, instead of just using the defined function, was also adding the previous calculated answer and showing the result. That is why, it always worked in the first iteration because the previous calculated answer was 0 since we had not calculated the answer at all. But on the second iteration, we were replacing the first number with the calculated answer, then using a number given by the user, doing the calculation they needed and then added the calculated answer on top. All because instead of using “=”, the IDE had suggested “+=” which I used. Here’s the insight, always be sure to read what you type no matter how easy you think the logic is.

All of this coding I defined under a new function and used a recursive loop so that the user can either continue calculating using the previous calculated answer or start from scratch if they did not want to include the previous calculations. Once this was done, I tidied up the code, commented out all the print statements I did not want the user to find, asked proper questions for the inputs needed from the user and I was done for the day.

We started playing badminton with friends a week ago and decided to go with a big group of unknown people who play regularly. Its our first time to play continuously for a couple of hours, especially with people who play semi professionally. But since it is Friday and we have the weekend to rest, we decided to push ourselves and put ourselves out there no matter how bad we might be in comparison to the others. Wish us luck and a happy weekend to you! See you on Monday.