Python:创建表时,“none”是什么意思?
我在尝试制作一个简单的乘法表,但总是出现一些“none”。这些“none”是什么意思,我该怎么去掉它们呢?
>>> 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
3 个回答
1
我也遇到过这个“none”的问题。我写了一个函数,然后想在另一个函数里用它。但是我写的是'print calculation',其实应该是'return calculation'。所以第二个函数就出现了'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
1
在你的函数 printT() 中,print M(w) 并没有返回任何东西。在 Python 里,函数默认返回的值是 None,这就是你在循环中看到的输出。
你只需要把你的函数改写成这样:
def printT():
w = 1
while w <= 6:
M(w)
w += 1
8
把 print M(w)
改成 M(w)
就可以了。你现在是在打印 M(w)
的返回值,而这个返回值是 None
,因为你在那个函数里没有返回任何东西。