Learn Python with Dhawal -8-Chapter


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.


  1. We have to make a calculator.
  2. It has to take user inputs
  3. It has to perform operations based on  user's choice
  4. It has to be done all within a single class.
  5. If we have to take inputs we would need input() method and some variables to catch input
  6. We have to make functions for each operation which would be called based on user input to perform a particular task
  7. We need to have input variables for taking the user input for performing operations
  8. We have to display the results as well
  9. We would need to ask what operation is to be performed.
  10. 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


  1. #creating an addition function
  2. def addition (a,b):
  3.     a=a+b
  4.     print ("The result of addition is ",a)
  5.     
  6. #creating a subtraction function
  7. def subtraction (a,b):
  8.     a=a-b
  9.     print ("The result of subtraction is ",a)
  10.     
  11. #creating a multiplication function
  12. def multiplication (a,b):
  13.     a=a*b
  14.     print ("The product is ",a)
  15.     
  16. #creating a division function
  17. def division (a,b):
  18.     a=a/b
  19.     print ("The quotient after divsion is ",a)

  20. #creating a modulus division function
  21. def moddiv (a,b):
  22.     a=a%b
  23.     print ("The remainder after division is ",a)

  24. #Once we are done with Creating functions
  25. # First we would need to make a variable to see if the program should run again or not once an operation is finished.
  26. choice=1

  27. #Putting 1 or 0 inside conditions means telling the condition is true or false respectively.
  28. while(choice):
  29.     #take both input numbers
  30.     a=int(input("Enter the a "))
  31.     b=int(input("Enter the b "))
  32.     #printing both numbers
  33.     print("The a is ",a," and b is ",b)
  34.     #now we have taken a variable which will let user enter the choice again if he/she had entered a wrong option.
  35.     run_loop=1
  36.     #since the default value of run_loop is kept 1 it will mean true and while loop will run

  37.     while(run_loop):
  38.         #take the choice from the user what what operation they want to peform.
  39.         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"))
  40.         print ("You chose option number ",c)
  41.         if(c==1):
  42.             #a function is called depending on the choice made
  43.             addition(a,b)
  44.             #making run_loop 0 means next time it won't run while loop and it will get out of this while loop.
  45.             run_loop=0
  46.         elif(c==2):
  47.             subtraction(a,b)
  48.             run_loop=0
  49.         elif(c==3):
  50.             multiplication(a,b)
  51.             run_loop=0
  52.         elif(c==4):
  53.             division(a,b)
  54.             run_loop=0
  55.         elif(c==5):
  56.             moddiv(a,b)
  57.             run_loop=0
  58.         else:
  59.             #we haven't made any run_loop changes as we want user to enter the valid choice again for the same set of numbers.
  60.             print("sorry invalid input, enter the correct choice again.")

  61.     #once the operation is over, we can ask user if they want to do more calculations or just end the program.
  62.     choice=int(input("Enter 1 to continue and 0 to end the program\n"))
  63. #This will print a thanks note and end the program.
  64. print ("Thank You for Using our calculator program, you'd need to run the program again to perform more calculations.")
Output :


  1. Enter the a 10
  2. Enter the b 5
  3. The a is  10  and b is  5
  4. Enter the correct number for the operation you want to perform.
  5. 1. Addition (a+b)
  6. 2. Subtraction (a-b)
  7. 3. Multiplication (a*b)
  8. 4. Division (a/b)
  9. 5.Modulus Divison (a%b)
  10. 1
  11. You chose option number  1
  12. The result of addition is  15
  13. Enter 1 to continue and 0 to end the program
  14. 1
  15. Enter the a 10
  16. Enter the b 5
  17. The a is  10  and b is  5
  18. Enter the correct number for the operation you want to perform.
  19. 1. Addition (a+b)
  20. 2. Subtraction (a-b)
  21. 3. Multiplication (a*b)
  22. 4. Division (a/b)
  23. 5.Modulus Divison (a%b)
  24. 9
  25. You chose option number  9
  26. sorry invalid input, enter the correct choice again.
  27. Enter the correct number for the operation you want to perform.
  28. 1. Addition (a+b)
  29. 2. Subtraction (a-b)
  30. 3. Multiplication (a*b)
  31. 4. Division (a/b)
  32. 5.Modulus Divison (a%b)
  33. 4
  34. You chose option number  4
  35. The quotient after divsion is  2.0
  36. Enter 1 to continue and 0 to end the program
  37. 0
  38. 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!
Share :

Are you getting regular fresh content updates? If not click on the button below.

0 comments:

Post a Comment

Do comment and share your thoughts.