Py学习  »  Python

这里的菜鸟。。。需要Python代码总结方面的帮助吗

Grade-a-j • 3 年前 • 1177 次点击  

“你好,[名字]!”或者“你好!” 根据输入

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

我之前已经完成了类似的练习测试,但我只是在这里画一个空白。 我们将非常感谢您对正确代码的任何帮助,以及您是如何获得这些代码的。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133605
 
1177 次点击  
文章 [ 2 ]  |  最新文章 3 年前
tdelaney
Reply   •   1 楼
tdelaney    3 年前

assert 这没什么意义。您只需要根据 name 。您可以从向 名称 参数,调用方可以选择完全不提供名称,然后进行简单的真实性测试。

def say_hello(name=None):
    if name:
        return f"Hello, {name}"
    else:
        return "Hello there!"
notoriousjere
Reply   •   2 楼
notoriousjere    3 年前

如果给定了一个名称且该名称不为空(“计算结果为False”),则将填充该名称

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