IF statement and return python

Viewed 2499

I am trying to design a function that returns whether a or b is longer but I'm having a syntax error.


def get_longer(a:str, b:str):

    return a if len(a) >= len(b) else return b 

I have tried with a print statement and it is working however I need it to work with a return statement.

Any suggestions?

2 Answers

You have an extra return statement

def get_longer(a:str, b:str):
    return a if len(a) >= len(b) else b 

You can try the following code.

def get_longer(a:str, b:str) -> str:
    if (len(a) >= len(b)):
        return a
    else:
        return b
Related