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.
- speak('english') - expected output should be
english - speak('english', 'french') - expected output should be
englishandfrench - speak(34) - I expect the function to throw an error
languageswill be a list, not a string, by the way.speak()- i.e. show the parameter(s) you’re passing*languages? As others commented, you should show how you are calling your function and what is the expected output for each case.