**CHAPTER 8**

**Before we move on forward and learn the concepts of OOP ( Object Oriented Programming) and learn how it is different to implement the concept while programming multiple class long programs, let's first get a full understanding of using and playing with functions inside a single class or you can say inside a single file code.**

__PROBLEM STATEMENT__

*Make a calculator using functions to take input from user and perform the operations as per user choice in a single class.*Now before we begin, if you remember how we had made a addition and subtraction function in previous chapters, it is very close to on that lines. Before we move ahead and code together you can try on your own and see if you can make one on your own without my help at all. If you tried it, do comment and send me an email with your code if you want my feedback on it. Obviously I'd try to help as much as I can.

Now If you tried or you just want to go along with me here is how we are going to do it. I am explaining how we are going to make it and then I'll explain the actual coding example with the desired output.

First step would be to analyse the problem statement and then try to make constraints which will help you to set a kind of a rough boundary and get you a very rough idea of what you actually need in your program. You can take a pencil and paper and try out yourself before reading ahead and then you can cross check to self evaluate and see how much of it did you guessed correctly. Also, usually you don't do guess work when you are dealing with technical specifics but since we are learning here we can always afford to make mistakes and learn in much better manner.

So as per out first step we want to have a rough idea of what exactly we have to make or keep in mind will writing the code. Here's a few things I deduced from the problem statement.

- We have to make a calculator.
- It has to take user inputs
- It has to perform operations based on user's choice
- It has to be done all within a single class.
- If we have to take inputs we would need input() method and some variables to catch input
- We have to make functions for each operation which would be called based on user input to perform a particular task
- We need to have input variables for taking the user input for performing operations
- We have to display the results as well
- We would need to ask what operation is to be performed.
- We need something to check what input user has give and then check if it fits in our options.

Here these are a very few points but as we know that this code is to do as much as we can, I am going to add some more features and functionalities to the code just to show you how much can you do even from a simple problem statement. But this depends all in your creativity and understanding of logic. I'll explain everything at the end but until then, lets begin.

**Note :**

*# is used to note down a comment in middle of the code, commenting in a code is considered as a very good pro habit since at times if you are working on project and someone has to take you over for some other reason it just improves the readability of the overall code as you don't always have to explain what your logic was to someone else.*

*# is used to have a single line comment. But there are various ways to post a multiple line comment as it differs from interpreter to interpreter as every installation has its own way but # is used in every command line based as well as software interface. If you press enter then it is no longer a single line comment keep that in mind. As you'd notice it with the lines of code shown in the example below. Do notice the line numbers.*

*Another thing is that the backslash operands like \n and \t are used to get the pointer to next line in output and to a tab space later respectively. You might have noticed it in the previous pattern printing chapters.*

*I have explained most of the code with the help of the comments and rest I feel you are smart enough to deduce the other basic things. If not you can always comment and ask a doubt. I'll be more than happy to answer. Also take your sweet time to understand this simple code.*

**Code**

**Input**

- #creating an addition function
- def addition (a,b):
- a=a+b
- print ("The result of addition is ",a)
- #creating a subtraction function
- def subtraction (a,b):
- a=a-b
- print ("The result of subtraction is ",a)
- #creating a multiplication function
- def multiplication (a,b):
- a=a*b
- print ("The product is ",a)
- #creating a division function
- def division (a,b):
- a=a/b
- print ("The quotient after divsion is ",a)
- #creating a modulus division function
- def moddiv (a,b):
- a=a%b
- print ("The remainder after division is ",a)
- #Once we are done with Creating functions
- # First we would need to make a variable to see if the program should run again or not once an operation is finished.
- choice=1
- #Putting 1 or 0 inside conditions means telling the condition is true or false respectively.
- while(choice):
- #take both input numbers
- a=int(input("Enter the a "))
- b=int(input("Enter the b "))
- #printing both numbers
- print("The a is ",a," and b is ",b)
- #now we have taken a variable which will let user enter the choice again if he/she had entered a wrong option.
- run_loop=1
- #since the default value of run_loop is kept 1 it will mean true and while loop will run
- while(run_loop):
- #take the choice from the user what what operation they want to peform.
- c=int(input("Enter the correct number for the operation you want to perform.\n1. Addition (a+b)\n2. Subtraction (a-b)\n3. Multiplication (a*b)\n4. Division (a/b)\n5.Modulus Divison (a%b)\n"))
- print ("You chose option number ",c)
- if(c==1):
- #a function is called depending on the choice made
- addition(a,b)
- #making run_loop 0 means next time it won't run while loop and it will get out of this while loop.
- run_loop=0
- elif(c==2):
- subtraction(a,b)
- run_loop=0
- elif(c==3):
- multiplication(a,b)
- run_loop=0
- elif(c==4):
- division(a,b)
- run_loop=0
- elif(c==5):
- moddiv(a,b)
- run_loop=0
- else:
- #we haven't made any run_loop changes as we want user to enter the valid choice again for the same set of numbers.
- print("sorry invalid input, enter the correct choice again.")
- #once the operation is over, we can ask user if they want to do more calculations or just end the program.
- choice=int(input("Enter 1 to continue and 0 to end the program\n"))
- #This will print a thanks note and end the program.
- print ("Thank You for Using our calculator program, you'd need to run the program again to perform more calculations.")

**Output :**

- Enter the a 10
- Enter the b 5
- The a is 10 and b is 5
- Enter the correct number for the operation you want to perform.
- 1. Addition (a+b)
- 2. Subtraction (a-b)
- 3. Multiplication (a*b)
- 4. Division (a/b)
- 5.Modulus Divison (a%b)
- 1
- You chose option number 1
- The result of addition is 15
- Enter 1 to continue and 0 to end the program
- 1
- Enter the a 10
- Enter the b 5
- The a is 10 and b is 5
- Enter the correct number for the operation you want to perform.
- 1. Addition (a+b)
- 2. Subtraction (a-b)
- 3. Multiplication (a*b)
- 4. Division (a/b)
- 5.Modulus Divison (a%b)
- 9
- You chose option number 9
- sorry invalid input, enter the correct choice again.
- Enter the correct number for the operation you want to perform.
- 1. Addition (a+b)
- 2. Subtraction (a-b)
- 3. Multiplication (a*b)
- 4. Division (a/b)
- 5.Modulus Divison (a%b)
- 4
- You chose option number 4
- The quotient after divsion is 2.0
- Enter 1 to continue and 0 to end the program
- 0
- Thank You for Using our calculator program, you'd need to run the program again to perform more calculations.

I hope you have a better understand of using functions and are able to build your own logic if a particular problem statement is been given. If you want some practise questions then always feel free to comment down below, I'd be happy to provide you with materials.

In Next chapter we will learn about the concept of OOP and how to its related to python.

**~*~*~*~**

*This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.***~*~*~*~**

Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

**Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!**

## 0 comments:

## Post a Comment

Do comment and share your thoughts.