Page 212 - Computer Science V2.0 Class 11
P. 212

Program 8.1: To display a triangle, a rhombus, and a triangle again.

                01 #Print a triangle
                02 print("     *  ")
                03 print("    ***  ")
                04 print("   *****  ")
                05 print("  *******  ")
                06 print()
                07 #Print a rhombus
                08 print("     *  ")
                09 print("    ***  ")
                10 print("   *****  ")
                11 print("  *******  ")
                12 print("   *****  ")
                13 print("    ***  ")
                14 print("     *  ")
                15 print()
                16 #Print a triangle
                17 print("     *  ")
                18 print("    ***  ")
                19 print("   *****  ")
                20 print("  *******  ")
              On executing the above program, Python responds with the following output:
                   *
                  ***
                 *****
                *******


                   *
                  ***
                 *****
                *******
                 *****
                  ***
                   *



                   *
                  ***
                 *****
                *******
              Fig 8.1: Output on execution of Program 8.1

              The above program suffers from the following two drawbacks:
              ●  Although the printing the triangle and rhombus are independent activities, it is not   clearly visible in the program.
              ●  The code for printing the triangle appears twice.
              We already know that the functions provide a systematic approach to problem-solving by dividing a complex problem
              into simpler sub-problems, developing solutions for each sub-problem, and combining the individual solutions of the
              sub-problems to solve the original problem. So, to overcome the above drawbacks, we will develop functions named
              triangle and rhombus to print a triangle and a rhombus, respectively. A Python program is a global frame in which
              functions and statements appear.




               198   Touchpad Computer Science (Ver. 2.0)-XI
   207   208   209   210   211   212   213   214   215   216   217