Page 167 - Computer Science Class 11 With Functions
P. 167

The execution of program 7.1 generates an error because input() returns a string value and the binary operation
            multiplication cannot be performed on strings. So, execution of  the statement, square = num * num, results in an
            error. Of course, if the data type of the value being returned by input() is converted to an integer prior to assigning
            it to num, using int(), then an arithmetic operation can be performed on num. The revised version of program 7.1
            given below. It correctly displays the square of number 5 as output.

            Program 7.2 Revision of program 7.1

              01 # Objective: To accept a number and display its square.
              02 num = int(input("Enter a number:  "))  # explicit type conversion
              03 square = num * num
              04 print("The square of the entered number is", square)
            Sample Output:
             >>> Enter a number : 5
                 The square of the entered number is 25
            Table 7.8 lists some of the functions that can be used for explicit type conversion in Python. In this table, the name
            num denotes a numeric object.
                                         Table 7.8: Functions for explicit type conversion in Python

                     Function                                         Description
               int(num)              Converts num to an integer

               float(num)            Converts num to a floating-point number
               str(num)              Converts num to a string on which no arithmetic calculations can be done
               chr(num)              Converts num to str, if in suitable range

               ord(character)        Transforms an ASCII character to its ASCII code and a Unicode character to its Unicode.

            7.9 Types of Errors

            Various  types  of  errors  may  be  generated  while  creating  or  executing  a  program.  The  process  of  identifying  and
            removing such errors is known as debugging. The errors are called bugs. The errors in a program broadly fall in three
            categories:
            ● Syntax Error                   ● Logical Errors             ● Runtime Errors

            7.9.1 Syntax Errors

            Every programming language is described by a set of rules, collectively called the syntax of the language. If a program
            violates a syntax rule, the interpreter/compiler will flash an error. Such errors are known as syntax errors. Thus, a Python
            interpreter will execute only those statements that are syntactically correct. When a  syntax error is encountered, the
            execution of the program stops, and the error message is displayed. The execution of the program resumes only after
            the  error is rectified. For example,

             >>> print("hello)
                 SyntaxError: incomplete input
            in  the statement  print("hello),  the  closing  quotation  marks  are  missing,  leading  to  a  syntax  error.  Python
            interpreter noted the beginning of string, but could not find the closing quotation marks. A syntax error must be
            removed before Python interpreter can proceed.



                   Python Interpreter gives a brief description of each error, as seen in the above example.






                                                                                        Data Types and Operators  165
   162   163   164   165   166   167   168   169   170   171   172