Python:创建一个表,“none”是什么意思?

2024-09-26 22:13:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着建立一个基本的乘法表,但我一直得到这些“无”字。它们是什么意思?我怎样才能去掉它们?在

    >>> def M(n):
...     i = 1
...     while i <= 6:
...             print i*n, '\t',
...             i = i +1
...     print

    >>> def printT():
...     w = 1
...     while w <= 6:
...             print M(w)
...             w = w + 1
... 

>>> printT()
1   2   3   4   5   6   
None
2   4   6   8   10  12  
None
3   6   9   12  15  18  
None
4   8   12  16  20  24  
None
5   10  15  20  25  30  
None
6   12  18  24  30  36  
None

Tags: nonedefprintwhile乘法表printt
3条回答

我也没有这种事。我键入了一个函数,并试图在另一个函数中使用它。但我写了“打印计算”。应该是“收益计算”。所以第二个函数给出'none'错误。检查我的代码:

def factoriel(n):
    i = 1
    calculation = float(n)     #if there is not 'float', the type would be int

    while i < n:
        calculation = calculation * i
        i += 1
    return calculation        #it was print at first try, so i got 'none'

x = factoriel(5)

print x, type(x)              #to check the result

在函数print()中,print M(w)没有返回任何内容。python中函数的隐式返回值为None,这是在循环期间打印的值。在

只需像这样重新编写函数:

def printT():
    w = 1
    while w <= 6:
        M(w)
        w += 1

print M(w)替换为M(w)。您正在打印M(w)的返回值None,因为您没有从该函数返回任何内容。在

相关问题 更多 >

    热门问题