Elegant code

Can someone help me to shorten the function to one line please?

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

My apologies that I don’t know how to indent lines in this Help forum.

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

Fascinating. I entered your elegant code in and the output was correct and the same as my also correct but longer code. But why is the output of your elegant code a True value … and not, for example, the names given to the variables? For reference, I am looking at Python Code Challenges: Control Flow (Advanced) Question #2, link below …


A comparison is boolean, so must yield either True or False as there is no other value in the bool class.

Even if we changed up the logic to use and and or we would not return the names of the variables, but one of the two values.

>>> your_name = 'Wee Gillis'
>>> my_name = 'Roy'
>>> print (your_name and my_name)
>>> print (your_name or my_name)
Wee Gillis

Read up on 'short-circuiting of AND and OR expressions.

I’ll take a look at the page you linked and return, presently.

Okay, I’m looking at the page if you have any more questions.

1 Like