实用:python编写一个函数,能够接受一个参数n,n为正整数,上三角和下三角打印.要求数字必须对齐

案例一:

def fn(n):
    lst = [ i for i in range(n,0,-1)]
    print(lst)
    c = len(str(n))-1
    w1 = c+2
    w2 = c+3
    for i in range(n+1,1,-1):
        space = [' ' for i in range(i-2)]
        space.extend(lst[i-2:])
        for i in range(-n,0):
            if i > -(10**c):
                print('{1:<{0}}'.format(w1,space[i]),end='')
            else:
                print('{1:<{0}}'.format(w2,space[i]),end='')
        else:
            print()

fn(11)

运行结果:

[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
                                1  
                             2  1  
                          3  2  1  
                       4  3  2  1  
                    5  4  3  2  1  
                 6  5  4  3  2  1  
              7  6  5  4  3  2  1  
           8  7  6  5  4  3  2  1  
        9  8  7  6  5  4  3  2  1  
    10  9  8  7  6  5  4  3  2  1  
11  10  9  8  7  6  5  4  3  2  1 

案例变形1:

def fn(n):
    tail = "  ".join((str(i) for i in range(n,0,-1)))
    print(tail)
    width = len(tail)
    for i in range(1,n):
        print("{:>{}}".format("  ".join([str(j) for j in range(i,0,-1)]),width))
    print(tail)
fn(11)

运行结果:

11  10  9  8  7  6  5  4  3  2  1
                                1
                             2  1
                          3  2  1
                       4  3  2  1
                    5  4  3  2  1
                 6  5  4  3  2  1
              7  6  5  4  3  2  1
           8  7  6  5  4  3  2  1
        9  8  7  6  5  4  3  2  1
    10  9  8  7  6  5  4  3  2  1
11  10  9  8  7  6  5  4  3  2  1

案例二:

def fn(n):
    lst = [ i for i in range(n,0,-1)]
    print(lst)
    c = len(str(n))-1
    w1 = c+2
    w2 = c+3
    for i in range(n):
        nums = lst[i:]
        space = ['' for _ in range(i)]
        line = space.extend(nums)
        for i in range(-n,0):
            if i > -(10**c):
                print('{1:<{0}}'.format(w1,space[i]),end='')
            else:
                print('{1:<{0}}'.format(w2,space[i]),end='')
        else:
            print()

运行结果:

fn(11)
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
11  10  9  8  7  6  5  4  3  2  1  
    10  9  8  7  6  5  4  3  2  1  
        9  8  7  6  5  4  3  2  1  
           8  7  6  5  4  3  2  1  
              7  6  5  4  3  2  1  
                 6  5  4  3  2  1  
                    5  4  3  2  1  
                       4  3  2  1  
                          3  2  1  
                             2  1  
                                1 

案例变形:

def fn(n):
    tail = "  ".join(str(i) for i in range(n,0,-1))
    width = len(tail)
    for i in range(n,0,-1):
        print("{:>{}}".format("  ".join([str(j) for j in range(i,0,-1)]),width))

fn(11)

运行结果:

11  10  9  8  7  6  5  4  3  2  1
    10  9  8  7  6  5  4  3  2  1
        9  8  7  6  5  4  3  2  1
           8  7  6  5  4  3  2  1
              7  6  5  4  3  2  1
                 6  5  4  3  2  1
                    5  4  3  2  1
                       4  3  2  1
                          3  2  1
                             2  1
                                1

你可能感兴趣的:(python,Python学习记录)