Page 297 - AI Ver 1.0 Class 9
P. 297
SyntaxError: EOL while scanning string literal
a = 5
b = 10
c = a + b
SyntaxError: unexpected indent
print("Python"))
SyntaxError: unmatched ')'
Logical Error
This kind of error is difficult to find since the program will run correctly but the desired output is not achieved.
This happens if we give a wrong formula for the calculation to be done, write wrong logic for the problem to be
solved through the code. 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 error occurs during the execution of a program like wrong input or output errors, undefined object
errors, division by zero errors. This type of error will halt the program execution unexpectedly.
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
• Selection
• Iterative
Let us learn about these in detail.
Sequential Statements
A step-by-step process of execution of 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 till 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
Introduction to Python 295

