打印函数结果,但打印了两行,其中一行是”None”。

huangapple go评论128阅读模式
英文:

Print function result but two line with a None is printed

问题

def same_name(your_name, my_name):
  if your_name == my_name:
    return True
  else:
    return False
  
print(same_name("Colby", "Colby"))
print(same_name("Tina", "Amber"))
英文:

> Create a function named same_name() that has two parameters named
> your_name and my_name.
>
> If our names are identical, return True. Otherwise, return False.

My code:

def same_name(your_name, my_name):
  if your_name == my_name:
    print(True)
  else:
    print(False)
  
print(same_name("Colby", "Colby"))
print(same_name("Tina", "Amber"))

The output is:

print(same_name("Colby", "Colby"))
# True
# None
print(same_name("Tina", "Amber"))
# False
# None

But the expected output is:

print(same_name("Colby", "Colby"))
# True
print(same_name("Tina", "Amber"))
# False

Where None is coming from?

答案1

得分: 2

return, not print

通常错误的做法是使用 print 而不是 return。如果一个函数需要返回一个值,print 是不会做到的,它只是为了用户使用,通常用于调试。由于你的函数中没有返回语句,所以默认情况下函数返回 None。在 print(same_name("Colby", "Colby")) 中,你打印了函数的返回值,即 None,所以你打印了两次(第一次是在函数内部),带有 None 值。

  if your_name == my_name:
    return True
  else:
    return False

通常有两种类型的练习,一种是要求你创建/编写一个函数,另一种不需要。

当你不需要函数时:

以你的练习为例:

> 要求用户输入两个名字。如果第一个名字等于第二个名字,则打印 True,否则打印 False

预期的代码应该像这样:

name_1 = input('name?')
name_2 = input('name?')
if name_1 == name_2:
    print(True)
else:
    print(False)

或者更简洁的替代方法:

name_1 = input('name?')
name_2 = input('name?')
print(name_1 == name_2)

当你需要一个函数时:

以你的练习为例:

> 创建一个名为 same_name 的函数,它接受两个名字。如果第一个名字等于第二个名字,返回 True,否则返回 False

预期的代码应该像这样:

def same_name(name_1, name_2):
    if name_1 == name_2:
        return True
    else:
        return False

或者更简洁的替代方法:

def same_name(name_1, name_2):
    return name_1 == name_2

如果你使用函数运行程序,将不会打印任何内容,即使你像 same_name("Colby", "Colby") 这样调用函数。这是因为 return 不会打印任何内容,它会退出函数并返回一个值。这就是为什么他们在函数调用周围加上了 print,以打印返回的值,查看结果并确保它与预期的行为匹配的原因。

注意:推荐的实践是返回条件的结果

推荐的实践是返回条件的结果而不是使用 if 进行分支处理:

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

return, not print

It's a common mistake to print instead of return. If a function need to return a value, print won't do it, it's just for the user, commonly used to debug. Since there is no return statement in your function, the function return None by default. In print(same_name("Colby", "Colby")), you print the returned value of the function, which is None, so you print a second time (the first one is in the function) with a None value.

  if your_name == my_name:
    return True
  else:
    return False

Usually, there is two types of exercises, those who ask you to create/code a function, and those who doesn't.

When you don't need a function:

Example with your exercise:

> Ask two names to the user. If the first name is equal to the second
> name, print True, if not, print False.

The expected code would be something like this:

name_1 = input('name?')
name_2 = input('name?')
if name_1 == name_2:
    print(True)
else:
    print(False)

or the shorter alternative:

name_1 = input('name?')
name_2 = input('name?')
print(name_1 == name_2)

When you do need a function:

Example with your exercise:

> Create a function called same_name that takes two names. If the first name are equal
> to the second name, return True, if not, return False.

The expected code would be something like this:

def same_name(name_1, name_2):
    if name_1 == name_2:
        return True
    else:
        return False

or the shorter alternative:

def same_name(name_1, name_2):
    return name_1 == name_2

If you run the program with the function, you won't print anything, even if you call the function like same_name("Colby", "Colby"). It's because return doesn't print anything, it quit the function with a value. That's why they surrounded the function call with print, to print the returned value, see the result and make sure it match the expected behavior.

The recommended practice is to return the condition result instead of branching with if:

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

huangapple
  • 本文由 发表于 2023年3月9日 17:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定