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

本文介绍如何用Python编写一个函数,该函数接收一个正整数n作为参数,然后分别打印出上三角形和下三角形,确保数字对齐。通过展示不同案例和运行结果,详细解释了函数的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

案例一:

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值