Computer Science
After importing the above module, some of its functions are executed as per following statements. Find errors, if any:
(a) square(print 3)
(b) basic.div()
(c) basic.floordiv(7.0, 7)
(d) div(100, 0)
(e) basic.mul(3, 5)
(f) print(basic.square(3.5))
(g) z = basic.div(13, 3)
Python Libraries
6 Likes
Answer
(a) square(print 3) — print function should not be there as parameter in the function call. So the correct function call statement is square(3). Also, to call square function without prefixing the module name, it must be imported as from basic import square.
(b) basic.div() — The div() function requires two arguments but none are provided. So the correct statement is basic.div(x, y).
(c) basic.floordiv(7.0, 7) — There is no error.
(d) div(100, 0) — This will result in a runtime error of ZeroDivisionError as we are trying to divide a number by zero. The second argument of the function call should be any number other than 0. For example, div(100, 2). Also, to call div function without prefixing the module name, it must be imported as from basic import div.
(e) basic.mul(3, 5) — There is no error.
(f) print(basic.square(3.5)) — There is no error.
(g) z = basic.div(13, 3) — There is no error.
Answered By
1 Like
Related Questions
A function checkMain() defined in module Allchecks.py is being used in two different programs. In program 1 as
Allchecks.checkMain(3, 'A')and in program 2 ascheckMain(4, 'Z'). Why are these two function-call statements different from one another when the function being invoked is just the same ?Given below is semi-complete code of a module basic.py :
# """...............""" def square(x): """...............""" return mul(x, x) ...............mul(x, y): """...............""" return x * y def div(x, y): """...............""" return float(x)/y ...............fdiv(x, y)............... """...............""" ...............x//y def floordiv(x, y)............... ...............fdiv(x, y)Complete the code. Save it as a module.
Import the above module basic.py and write statements for the following :
(a) Compute square of 19.23.
(b) Compute floor division of 1000.01 with 100.23.
(c) Compute product of 3, 4 and 5. (Hint. use a function multiple times).
(d) What is the difference between basic.div(100, 0) and basic.div(0, 100)?
Suppose that after we import the random module, we define the following function called
diffin a Python session :def diff(): x = random.random() - random.random() return(x)What would be the result if you now evaluate
y = diff() print(y)at the Python prompt ? Give reasons for your answer.