4

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
  • 1
    This is not an if statement, it is a conditional expression. you can't have any statements in a conditional expression. Note, print isn't a statement, it is a function. You want return a if len(a) >= len(b) else b. Or just write out the full if-else statement Commented May 26, 2020 at 4:07
  • 2
    max(a, b, key=len) Commented May 26, 2020 at 4:19

2 Answers 2

6

You have an extra return statement

def get_longer(a:str, b:str):
    return a if len(a) >= len(b) else b 
Sign up to request clarification or add additional context in comments.

Comments

0

You can try the following code.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.