python 定义函数,打印一个上下左右的4分图

自定义函数代码:

def p4(x):
    for j in range(x+1):
        for i in range(x+1):
            if j > i and j > x - i:
                print('下'.rjust(2), end='')
            elif j > i and j < x - i:
                print('左'.rjust(2), end='')
            elif j < i and j < x - i:
                print('上'.rjust(2), end='')
            elif j < i and j > x - i:
                print('右'.rjust(2), end='')
            else:
                print(' ', end='')
        print()

 

调用函数

p4(11)

 

打印效果如下:

 

原文地址:https://www.cnblogs.com/shanger/p/12925267.html