In: Computer Science
Respond to the following in a minimum of 175 words:
One of the most important concepts of programming is handling input and output. The following activity will allow you to get familiar with this concept specifically when using PYTHON.
Write a function to add two values and display the results. (Please show me where indentation goes as well, I'm not only seeking guidance on answers I'm trying to understand this so I can learn).
Discuss the steps in your thought process as you created the code, any issues you encountered, and how you solved those issues.
Most of the programming languages supports provides curly braces to indicate the starting point and ending point of a block as well as for functions also. But in python the indentation is very useful for formation of a block, and this will help us to know which statements will execute under a specific block.
Let’s have a comparison
In C++ Programming language |
In Python |
int add(int a, int b) { int c; c=a+b; return c; } |
def add(a,b) c=a+b return c |
The above program will execute smoothly. |
Will yield to error as indentation error. Because compiler will unable to identify that which statements are included under the add function. So for this we have to allow a space before c=a+b and also before return c. So the rectified program will be |
For indentation minimum one space must required. But as a good programmer you may use 3 to 5 spaces as indent.
If in a block a number of statements are to be execute then all the statements should maintain same indent otherwise it will lead to error.
Example : THE BELOW PROGRAM WILL SHOW INDENTATION ERROR
AFTER PROVIDING INDENT AT LINE NUMBER 3 AGAIN IT WILL SHOW ERROR SINCE RETURN STATEMENT ALSO BELONGS TO THE METHOD
CORRECT PROGRAM CODE TO ADD TWO NUMBERS
The statements 5,6,7,9 are not indented since these does not belongs to the function add()