The code with if-else statements:
def sqrtt(argument):
if type(argument) == tuple or list or dict or set:
result = [int(numpy.sqrt(i)) for i in argument]
else:
result = int(numpy.sqrt(argument))
return result
The Response when I pass an integer as an argument:
Traceback (most recent call last):
File "C:\Users\anees\PycharmProjects\p5\main.py", line 39, in <module>
l2 = sqrtt(1)
File "C:\Users\anees\PycharmProjects\p5\main.py", line 33, in sqrtt
result = [int(numpy.sqrt(i)) for i in argument]
TypeError: 'int' object is not iterable
CodePudding user response:
You are checking if type(argument) is tuple
, and after that you are checking if list
or dict
or set
is True. If you would like to check if argument is one of those types you should do something like this:
def sqrtt(argument):
if type(argument) == tuple or type(argument) == list or type(argument) == dict or type(argument) == set:
result = [int(numpy.sqrt(i)) for i in argument]
else:
result = int(numpy.sqrt(argument))
return result
Or you can check if argument is in iterable types
def sqrtt(argument):
iterable_types = (tuple, list, dict, set)
if type(argument) in iterable_types:
result = [int(numpy.sqrt(i)) for i in argument]
else:
result = int(numpy.sqrt(argument))
return result
Sadly, your code won't work for dict objects