Page 393 - Ai_417_V3.0_C9_Flipbook
P. 393

For example,
                 To calculate the average:

                   p = marks1 + marks2 / 2 #instead of (marks1+marks2) / 2
                 Preceding code produces a wrong output due to the logical error in formula. Let us take another example. To
                 find the perimeter of rectangle,

                   p = 2 * l + b            #instead of p = 2 * (l + b)

                 Runtime Error
                 Runtime errors occur during the execution of a program and can result from various issues such as incorrect
                 input or output, undefined object errors, or division by zero errors. These errors can halt the program's execution
                 unexpectedly, making them challenging to debug.
                 Example of runtime error is:

                   a = int(input("enter first number"))
                   b = int(input("enter second number"))
                   c = a / b
                 If a = 5 and b = 0, then it will display:
                    ZeroDivisionError: division by zero

                         Control Statements


                 As you know that a program is a set of instructions given to a computer to do a specific task. The order of
                 execution of these instructions controls the flow of a program. There are three important ways to control the
                 flow of a program which are interpreted by any programming language in a structured manner. These are:
                    • Sequential Statements
                    • Selection Statements
                    • Iterative Statements
                 Let us learn about these in detail.


                         Sequential Statements

                 A step-by-step process of executing code is called sequential flow of control. This is the default flow of execution
                 of a program. It means that the computer will run the code one line at a time starting from line 1, followed by
                 line 2 and so on until it reaches the last line of the program.

                 Let us take an example of making a mango smoothie. The steps will be:
                 1.  Put mango pieces in a Mixer jar
                 2.  Add sugar
                 3.  Add milk
                 4.  Churn the mixer jar
                 5.  Pour in a glass
                 Let us do some more examples:

                  Program 1:  Input the first name and last name from the user and display the full name.
                 Algorithm

                 Step 1   Start
                 Step 2   Input FirstName, LastName



                                                                                        Introduction to Python  391
   388   389   390   391   392   393   394   395   396   397   398