Page 102 - Information_Practice_Fliipbook_Class11
P. 102

Program 4.2

        # Objective: To compute the quotient of two numbers
        #          : To understand runtime errors.
        #User inputs: numeric values for numerator and denominator
        numerator = int(input("Enter the numerator : "))
        denominator = int(input("Enter the denominator : "))
        quotient = numerator/denominator
        print("The quotient is :", quotient)

        Output 1: When the user enters denominator as 0

        Enter the numerator : 40
        Enter the denominator : 0
        Traceback (most recent call last):
          File "F:/Orange/runTimeErr.py", line 6, in <module>
            quotient = numerator/denominator
        ZeroDivisionError: division by zero


        Output 2 : When the user enters numerator as a string that cannot be interpreted as an int
        Enter the numerator : 22*22
        Traceback (most recent call last):
          File "C:\Users\ADMIN\Desktop\hhh.py", line 4, in <module>
            numerator = int(input("Enter the numerator : "))
        ValueError: invalid literal for int() with base 10: '22*22'

        Output 3: When the user enters suitable values

        Enter the numerator : 20
        Enter the denominator : 5
        The quotient is : 4.0
        Below, we give some more examples of runtime errors:
         >>> print(num)
              Traceback (most recent call last):
                File "<pyshell#0>", line 1, in <module>
                  print(num)
              NameError: name 'num' is not defined
        In the above example, the variable (name), num  is accessed for printing its value before it is assigned a value. So, a
        NameError is displayed because the name num is unknown to the interpreter. In the following example, the use of
        int and str objects is incompatible with the + operator, so the interpreter points  out a TypeError.

         >>> 10 + "Hello"
              Traceback (most recent call last):
                File "<pyshell#1>", line 1, in <module>
                  10 + "Hello"
              TypeError: unsupported operand type(s) for +: 'int' and 'str'

                 Match the following:
                 1.  Implicit type conversion            a.  Infinite loop
                 2.  Syntax Error                        b.  Trying to read a file that does not exist
                 3.  Logical Error                       c.  y = int(input("Enter a number"))
                 4.  Explicit Type conversion            d.  PRINT()
                 5.  Runtime Error                       e.  coercion



          88   Touchpad Informatics Practices-XI
   97   98   99   100   101   102   103   104   105   106   107