Python 函数返回无 [英] Python Function Returning None

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

问题描述

我非常简单的 python 函数在它的末尾返回None",我不太确定为什么.我看了一些其他的帖子,仍然无法弄清楚.任何帮助表示赞赏.

My very simple python function is returning 'None' at the end of it and I'm not sure quite why. I've looked at some other posts and still can't figure it out. Any help is appreciated.

这是我的代码:

def printmult(n):
    i = 1
    while i <= 10:
        print (n * i, end = ' ')
        i += 1

print(printmult(30))

推荐答案

因为在 Python 中,每个函数都会返回一个值,而 None 是您没有明确指定其他内容时返回的值.

Because in Python, every function returns a value, and None is what is returned if you don't explicitly specify something else.

期望 print(printmult(30)) 到底要做什么?您已经要求它评估 printmult(30),然后 print 计算由此产生的任何结果.某些事情必须由此产生,对吗?或者您可能希望引发某种异常?

What exactly did you expect print(printmult(30)) to do? You've asked it to evaluate printmult(30), and then print whatever results from that. Something has to result from it, right? Or were you maybe expecting some kind of exception to be raised?

请确保您了解打印和返回不是一回事.

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

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