Home > front end >  Rookie over here... Need assistance with a Python code conclusion
Rookie over here... Need assistance with a Python code conclusion

Time:05-05

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.

CodePudding user response:

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!"

CodePudding user response:

assert doesn't make much sense here. You just want to choose one of two return values based on the value of name. You can start with adding a default value to the name parameter which gives the caller the option of not supplying a name at all and then do a simple truth test.

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