How do I shorten this function's body to one line of code?

Here is the function:

def same_name(your_name, my_name):
  if (your_name == my_name):
    return True
    return False

Yeah… Just not sure how to do it at all. Any help would be appreciated.


Consider how your if statement is currently operating. What’s on the right hand side?


I know how it’s working tbh. Did the whole exercise with no hiccups. What’s the syntax look like for getting it all on one line though?

Heh, it’s supposed to be a hint :slight_smile:. What is the output of that expression on the right of the if?

1 Like

Well the output depends upon what arguments are put into the function.

For me this really is just a problem of syntax. I don’t visually know how this function could possibly all be condensed into one line. Like I don’t know what that would look like at all.

You might be overthinking it, there’s no special syntax necessary here. Just consider what a == b evaluates to, there’s only two options regardless of input. What is it you want at the end of the day?

The exercise wants me to condense the function’s body into one line, so that’s what I want I guess lol. Trying to learn stuff.

The output is either True or False, yes. But unfortunately that doesn’t solve my problem of how this can all go on one line if I don’t even know what that would look like anyway. To me it’s kind of like asking me to understand a function and then write one. Like sure, I can understand it, but without seeing it at all and not knowing that I first have to write def and then the function name etc. etc. I wouldn’t be able to write one.

No worries, there’s a reason it’s tagged as an additional/optional challenge since it isn’t immediately obvious. I will say the output of evaluating that expression looks very similar to what your function to return.

Yeah, the output is either True or False. I know. Going to google/ go on stackoverflow for the syntax.

As tgrtim said, there’s no special syntax. Just think of what print(a==b) would print.

Did it due to stackoverflow (conditional statements - one line if else condition in python - Stack Overflow)

Here’s the fun syntax I was looking after:

return True if your_name == my_name else False

One line! Thanks for the replies though everyone anyway :).

1 Like

Glad you found a way but I’d consider conditional expressions special syntax. You could enact codeneutrino’s suggestion to do a little testing with something that looks like-

def same_name(your_name, my_name):
  print(your_name == my_name)
1 Like

With return instead of print though otherwise you’d get “None”* as additional output.

And yes, that is nice, but I was thinking about condensing the lines in the original function (which included if and else). Maybe should have specified that some more as I think I caused a bit of confusion in this thread haha

Based on the placement in the course I think they expected return without conditional expressions making the if statement redundant to get a single line but you’ve learnt something new anyway so a worthwhile nose around anyway. Often handy for things like return anyway.

1 Like

To tag onto tgrtim’s answer, I’d add the following string method:

def same_name(your_name, my_name):
    return your_name.title() == my_name.title()

By applying a string method (could be anything really: .lower(), .upper() , etc), you’re sure to catch most fringe cases that would otherwise fail due to case sensitivity. This way “miKE” and “MIKe” or “mike” will all return true where they would have otherwise returned false.