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

查看:102
本文介绍了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.

以下是我的代码:

Here is my code:

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

print(printmult(30))


推荐答案

其他答案都做得很好指出错误的位置,即你需要明白打印和返回是不一样的。如果你希望你的函数返回一个你可以打印或存储在变量中的值,你需要这样的东西:

The other answers have done a good job of pointing out where the error is, i.e. you need to understand that printing and returning are not the same. If you want your function to return a value that you can then print or store in a variable, you want something like this:

def returnmult(n):
    i = 1
    result_string = ''
    while i <= 10:
        result_string += str(n * i) + ' '
        i += 1
    return result_string

print(returnmult(30))

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

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