Writing a program, especially for a beginner, is a challenging and daunting task! He/She knows the language but is not able to start. To make things simple, it is advisable to follow a process that is a correct program. Let us take a look at the Software Development Process,a systematic approach to writing a program.
SOFTWARE DEVELOPMENT PROCESS.
1. Formulate requirements
We have to understand the problem first, thoroughly, in order to program. Unless we dont understand the problem, how can we try to solve it?
2. Determine Specifications.
One has to describe exactly what our program is supposed to do. What are its inputs/outputs? We will not bother, at this stage, about how the program runs, but instead, what it will accomplish.
3. Create a Design
Here, the actual program is designed. The algorithm which will meet our specifications is designed here.
4. Implement the design
Now, we will translate our design into an actual Python(or any other language) program.
5. Testing/Debugging
This is an important step. Very rarely will your program run the first time correctly round or produce the results you expected. There are bound to be errors and at this stage,the program is carefully debugged,its underlying algorithm reviewed.
6. Maintenance.
The needs of the user are ever-evolving. So should your program! Many of the programs are rarely finished. They are constantly being updated.
Now, let us take an example to illustrate the process. Suppose we want to calculate the future value of an investment. Or, how much will an amount be 10 years from now? It depends on the initial amount and the interest rate. Given the principal and the interest rate, a program should be able to calculate the future value of an investment. Thus, we have defined the problem. Now, let us develop the specifications of the program.
As far as the inputs are concerned, we require the following:
1. The amount of money being invested(in rupees) ---- The Principal
2. The annual interest rate, expressed as a percentage--- APR
and the output of the program will be:
3. The value of the investment is 10 years in the future.
Our algorithm, which we will design now, depends upon the following formula:
Value after 1 year: Principal(1+APR), applied 10 times.
Algorithms are written in Pseudo Code. The following is the algorithm:
Print an introduction
Input the amount of the principal (principal)
Input the annual percentage rate (apr)
Repeat 10 times:
principal = principal * (1 + apr)
Output the value of principal
Note: The future value can be calculated in one step also. Here, the loop is being used just to illustrate the concept of programming in detail for beginners. If we know how to calculate the interest for one year, we can calculate the interest any number of years into the future.
Now, let us write the actual program in Python. Here is the complete program:
# futval . py
# A program to compute the value of an investment
# carried 10 years into the future
print ("This program calculates the future value")
print ("of a 10-year investment.")
principal = input ("Enter the initial principal: ")
apr = input ("Enter the annual interest rate: ")
for i in range (10) :
principal = principal * (1 + apr)
print ("The value in 10 years is: ", principal)
The actual running of the program:
This program calculates the future value
of a 10-year investment.
Enter the initial principal: 5000
Enter the annual interest rate: 0.08
('The value in 10 years is: ', 10794.624986363942)
Note: This lesson essentially focuses on an orderly software development process. To write a program in any language, we had to learn the elements of the language, its basic building blocks, syntax and semantics.