0

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.

  1. speak('english') - expected output should be english
  2. speak('english', 'french') - expected output should be english and french
  3. speak(34) - I expect the function to throw an error
8
  • 2
    languages will be a list, not a string, by the way. Commented Jul 9, 2021 at 21:34
  • 3
    @mkrieger1 A tuple, not a list. Commented Jul 9, 2021 at 21:38
  • 2
    Give an example of how you’re calling speak() - i.e. show the parameter(s) you’re passing Commented Jul 9, 2021 at 21:40
  • Why the asterisk in *languages ? As others commented, you should show how you are calling your function and what is the expected output for each case. Commented Jul 9, 2021 at 21:53
  • @Carlos I just updated the question with the ways I am calling the function and the expected output Commented Jul 9, 2021 at 21:59

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

passing a string as an argument to the above function executes the else statement. which shouldn't be the case. I expect the if statement to pass in such condition
@ChukwukaOkolie Edited the answer with your test cases
-1

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.

Comments

-1

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

2 Comments

The above function throws an error when I pass in str as an argument which shouldn't be the expected behaviour
I tried your solution using speak('english') and speak(50). the two function calls throws an error. I expect 1 to pass and 2 to fail

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.