Search code examples
pythonpython-3.xqualified

Rookie over here... Need assistance with a Python code conclusion


Complete this function to return either "Hello, [name]!" or "Hello there!" based on the input

def say_hello(name):
    name = "Hello there!"

    assert name != "Hello there!"
    # You can print to STDOUT for debugging like you normally would
    print(name)

    # but you need to return the value in order to complete the challenge  
    return "" # TODO: return the correct value

I've tried a couple of things but run into errors such as assertion error... I have completed practice test similar to this before but I'm just drawing a blank here. Any help with the proper code and how you came to it would be greatly appreciated.


Solution

  • This will populate the name if one is given and it is not empty ("" evaluates to False)

    def say_hello(name=None):
        return f"Hello, {name}!" if name else "Hello there!"