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

The program should have appropriate function calls and should display the output in the following format:
                      Name of Item:
                      Price per Unit:
                      Quantity:
                      Total Amount:

                                              NCERT Exercise Solutions


                 1.  Observe the following programs carefully, and identify the error:
                    (a)

                    def create (text, freq):
                           for i in range (1, freq):
                                print text
                    create(5)            #function call
                    (b)
                    from math import sqrt,ceil
                    def calc():
                        print cos(0)
                    calc()            #function call
                    (c)
                    mynum = 9
                    def add9():
                        mynum = mynum + 9
                        print mynum
                    add9()             #function call
                    (d)
                    def findValue(val1 = 1.1, val2, val3):
                          final = (val2 + val3)/ val1
                          print(final)
                    findvalue()        #function call
                    (e)
                    def greet():
                         return("Good morning")
                    greet() = message #function call
               Ans.  (a)  Error 1 : print text :- in this statement () is missing. Correct statement is print(text)
                       Error 2 : create(5) in this statement missing of one argument.
                       Correct statement is
                       create(5, 'orangebooks')
                    (b)  Missing of parentheses () in print statement.
                       Invalid call of cos() function. Function cos() not imported.


                       Corrected Code:
                       from math import cos
                       def calc():
                           print(cos(0))
                       calc()            #function call
                    (c)  Missing of parentheses () in print statement.
                        Another issue in the given code is related to variable scope. In the add9 function, you are trying to modify the value of the global
                       variable mynum by adding 9 to it. However, when you try to modify a global variable within a function, you need to explicitly declare
                       it as global using the global keyword. Here’s the corrected code:


               216   Touchpad Computer Science (Ver. 2.0)-XI
   225   226   227   228   229   230   231   232   233   234   235