Python100例 我的实现展示(61-65例)

本文展示了Python编程实现的61至65例,包括生成指定行数的杨辉三角形和图形绘制,如画椭圆和rectangle。通过代码示例,读者可以学习到如何在Python中进行基本的图形绘制和数学模式的输出。
摘要由CSDN通过智能技术生成

Python100例 我的实现展示(61-65例)


'''61、打印出杨辉三角形(要求打印出10行如下图)。'''


def test_exam_61():
    s = int(input("请输入一个正整数,程序将输出包含输入整数行的杨辉三角\n"))
    a = [[0 for col in range(s)] for row in range(s)]
    a[0][0] = 1
    for i in range(1, s):
        for j in range(i):
            if j == 0:
                a[i][j] = 1
                print("1", end="\t")
            elif j == i - 1:
                a[i][j] = 1
                print("1", end="")
            else:
                a[i][j] = a[i-1][j-1] + a[i-1][j]
                print(str(a[i][j]), end="\t")
        print()


'''62、查找字符串。'''


def test_exam_62():
    s = input("请输入1个字符串,作为查找样本字符串\n")
    x = input("请输入1个字符串,程序将从上一个字符串中找出本字符串所在的位置\n")
    index = s.find(x) + 1
    if index != -1:
        print("第二行输入的字符串在第一行输入字符串的第{0}个字母位置".format(str(index)))
    else:
        print("第二行输入的字符串在第一行输入字符串中找不到")


'''63、画椭圆。(完全不会,跟着搜索出来的题目模仿,模仿完好像也不会,哈哈)'''


def test_exam_63():
    import matplotlib.pyplot as plt
    from matplotlib.patches import Ellipse

    fig = plt.figure(0)
    ax = fig.add_subplot(111, aspect='equal')
    e = Ellipse(xy=(0, 2), width=1.81*2, height=0.94*2, angle=27.6)
    ax.add_artist(e)

    e.set_facecolor("green")
    plt.xlim(-2, 2)
    plt.ylim(0.5, 3.5)
    ax.grid(True)
    plt.title("50% Probablity Contour - Homework 4.2")
    plt.show()


'''64、利用ellipse 和 rectangle 画图。(完全不会,跟着搜索出来的题目模仿)'''


def test_exam_64():
    canvas = Canvas(width=400, height=600, bg='white')
    left = 20
    right = 50
    top = 50
    num = 15
    for i in range(num):
        canvas.create_oval(250 - right, 250 - left, 250 + right, 250 + left)
        canvas.create_oval(250 - 20, 250 - top, 250 + 20, 250 + top)
        canvas.create_rectangle(20 - 2*i, 20 - 2*i, 10 * (i+2), 10 * (i+2))
        right += 5
        left += 5
        top += 10
    canvas.pack()
    mainloop()


'''65、一个最优美的图案。(开放性问题没有参考答案,本题解题参考题目平台)'''


class PTS:
    def __init__(self):
        self.x = 0
        self.y = 0
points = []


def test_exam_65():
    screen_x = 400
    screen_y = 400
    canvas = Canvas(width=screen_x, height=screen_y, bg='white')

    AspectRatio = 0.85
    MAXPTS = 15
    h = screen_y
    w = screen_x
    xcenter = w / 2
    ycenter = h / 2
    radius = (h - 30) / (AspectRatio * 2) - 20
    step = 360 / MAXPTS
    angle = 0.0
    for i in range(MAXPTS):
        rads = angle * math.pi / 180.0
        p = PTS()
        p.x = xcenter + int(math.cos(rads) * radius)
        p.y = ycenter + int(math.sin(rads) * radius * AspectRatio)
        angle += step
        points.append(p)
    canvas.create_oval(xcenter - radius, ycenter - radius, xcenter + radius, ycenter + radius)
    for i in range(MAXPTS):
        for j in range(i, MAXPTS):
            canvas.create_line(points[i].x, points[i].y, points[j].x, points[j].y)
    canvas.pack()
    mainloop()

if __name__ == '__main__':

    # test_exam_61()
    # test_exam_62()
    # test_exam_63()
    # test_exam_64()
    test_exam_65()

test_exam_61运行结果如下图:
请输入一个正整数,程序将输出包含输入整数行的杨辉三角
10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1

Process finished with exit code 0

test_exam_63运行结果如下图:
在这里插入图片描述

test_exam_64运行结果如下图:
在这里插入图片描述

test_exam_65运行结果如下图:
在这里插入图片描述

注意:如果代码拷贝至自己程序文件中发现红色提示,会涉及到一些未引入和模块未安装的问题,请处理完相关内容后,可以完整运行。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿尔卑斯的畅想

欢迎打赏,一起每天进步一点点!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值