Reputation: 745
I have a very simple function, which takes as input a string, I tried to put a check on the function so that if the function arguments is not a string it will throw an error, else return the string.
The problem right now is that, when I pass an int
, I expect the function to display an error but it simply outputs a tuple of int
. What am I getting wrong here?
def speak(*languages):
"""Function takes a set of langs as input and returns a list of languages"""
try:
if type(languages) == str:
languages = list(languages)
except TypeError as error:
print(error, ' You need to pass in the right datatype')
else:
return languages
I also tried this.
def speak(*languages):
"""Function takes a set of langs as input and returns a list of languages"""
try:
if isinstance(languages, str):
languages = list(languages)
except TypeError as error:
print(error, ' You need to pass in the right datatype')
else:
return languages
I am calling the speak function the following ways.
english
english
and french
Upvotes: 0
Views: 108
Reputation: 548
I don't think a try-except is needed here. Maybe try something like this (following your reasoning):
def speak(*languages):
"""Function takes a set of langs as input and returns a list of languages"""
for lang in list(languages):
if not isinstance(lang, str):
print(' You need to pass in the right datatype')
return
else:
languages = list(languages)
return languages
print(speak('english')) # english
print(speak('english', 'french')) # english and french
print(speak(34)) # error
Upvotes: 0
Reputation: 3116
After you provided your example calls, it looks like you want to check all arguments for string-type and not one argument. Right?
You can use any
for that, which will be True if any of the list/generator value is true. In the generator comprehension you can perform the isinstance
over all (positional) arguments
def speak(*languages):
"""Function takes a set of langs as input and returns a list of languages"""
if any(not isinstance(lang, str) for lang in languages):
raise TypeError('You need to pass in the right datatype')
return list(languages)
>>> speak('english')
>>> speak('english', 'french')
>>> speak(34)
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "<string>", line 5, in speak
TypeError: You need to pass in the right datatype
Upvotes: 3
Reputation: 507
You aren't actually throwing an error here, to do so, you need to use raise
:
def speak(*languages):
"""Function takes a set of langs as input and returns a list of languages"""
if isinstance(languages, str):
languages = list(languages)
else:
raise TypeError('You need to pass in the right datatype')
return languages
Upvotes: -1
Reputation: 13589
Neither type(languages) == str
nor isinstance(languages, str)
will throw a TypeError
, so your except
will never be hit. You simply need an else
after your if
:
def speak(*languages):
"""Function takes a set of langs as input and returns a list of languages"""
if isinstance(languages, str):
return list(languages)
else:
raise TypeError('You need to pass in the right datatype')
Note: I don't think you want list(languages)
above - list("a str")
gives ['a', ' ', 's', 't', 'r']
. But I left it because it's not relevant to the question.
Upvotes: -1