函数返回None而不返回return语句 [英] Function returns None without return statement

查看:245
本文介绍了函数返回None而不返回return语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习(正在学习)函数参数在Python中的工作方式,并且在没有明显原因的情况下开始进行实验,

I just learned (am learning) how function parameters work in Python, and I started experimenting with it for no apparent reason, when this:

def jiskya(x, y):
    if x > y:
        print(y) 
    else:
        print(x) 

print(jiskya(2, 3))

输出:

>>>
2
None

None是从哪里来的?那是什么?

Where did the None come from? And what is it?

推荐答案

它是函数的返回值,您可以将其打印出来.如果没有return语句(或只是一个不带参数的return ) ,则将隐式return None添加到函数的末尾.

It's the return value of the function, which you print out. If there is no return statement (or just a return without an argument), an implicit return None is added to the end of a function.

您可能想返回函数中的值,而不是打印它们:

You probably want to return the values in the function instead of printing them:

def jiskya(x, y):
    if x > y:
        return y
    else:
        return x

print(jiskya(2, 3))

这篇关于函数返回None而不返回return语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆