我写了这段代码:你输入正方形的宽度,它会创建一个正方形,但是当正方形被创建时,None也会出现。为什么?
def square(width):
if width>=4 and width% 2 ==0:
inferior_superior(width)
for i in range(2):
side(width)
inferior_superior(width)
def inferior_superior(width):
print("+" + "-"*(width-2) + "+")
def side(width):
print('|' + ' '*(width-2) + '|')发布于 2016-10-24 02:55:42
注意,在python提示符下,square运行得很好:
>>> square(6)
+----+
| |
| |
+----+但是,如果打印square的结果,您将看到none:
>>> print(square(6))
+----+
| |
| |
+----+
None解决方案是单独使用square(n),而不是print(square(n))。
替代方案
如果您愿意,可以将返回值分配给square:
>>> def square(width):
... if width>=4 and width% 2 ==0:
... inferior_superior(width)
... for i in range(2):
... side(width)
... inferior_superior(width)
... return "Everything works A-OK."
... 现在,观察:
>>> print(square(6))
+----+
| |
| |
+----+
Everything works A-OK.https://stackoverflow.com/questions/40206611
复制相似问题