In Python, what kind of error is returned by the following code? (e.g. NameError, ValueError, IOError, etc.) def my_func(n1, n2): return n1 + n2 my_func(1, 2, 3)
Answers
Answer:
Type error
Explanation:
Let the code be tried the code with Python 3.6.4
Thus, after using the return in my_func ahead of the call of my_func, then you will get -
$ python3
Python 3.6.4 (default, Jan 7 2018, 15:53:53)
[GCC 6.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>> def my_func (n1, n2): return n1+n2
>> my_func (1, 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: my_func() takes 2 positional arguments but 3 were given
>>>
Therefore, the error returned is Type error.
Answer:
Explanation:
In Python, Type Error error is returned by the following code (e.g. NameError, ValueError, IOError, etc.): def my_func (n1, n2): return n1+n2 my_func (1, 2, 3). It would show the error termed as invalid syntax. The correct format would be the following code -
>>> def my_func (n1, n2):
... return n1+n2
... my_func (1, 2, 3)
Here, the my_func never gets called at all.