'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 1:[1]
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!"
Solution 2:[2]
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!"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | tdelaney |
