python图案右三角带有python的循环

发布于 2025-01-29 14:28:16 字数 736 浏览 2 评论 0原文

我需要以特定的形状和顺序打印数字,不允许 ,但是我不明白,我也无法在网上找到任何可以帮助我了解如何了解的我可以将图形移到右边,还是如何将其翻转。这是我目前拥有的代码。我想使用一个三角洲(8个空间)乘以数字的数字来向右移动形状,并将顶部形状后面的所有东西转换为空的空间,但不知道该怎么做

    i = 1
delta = 8
while i <= 5:
    j = 1
    while j <= i:
        print(j, end="")
        j += 1
    print()
    i += 1
a = 5
while a >= 1:
    b = 1
    while b <= a:
        print(b, end="")
        b += 1
    print()
    a -= 1

这是我得到的结果,我需要将第一个循环移至右边“在此处输入图像说明”

之后,我需要将较低的循环倒置,并在使它看起来与较低的循环一样,我能够使用一个循环进行操作,但是我不确定如何使用for lim lian ther循环进行操作并保持形状。

I need to print the numbers in a specific shape and order with a while loop, for loop is not allowed, but I don't understand nor can I find anything on the web that can help me understand how can I move the figure a bit to the right or how I can flip it around. Here is the code I have at the moment. I wanted to use a delta, which is 8 spaces multiplied with the digits behind to move the shape to the right and convert everything behind the top shape into empty spaces but don't know how to do it

    i = 1
delta = 8
while i <= 5:
    j = 1
    while j <= i:
        print(j, end="")
        j += 1
    print()
    i += 1
a = 5
while a >= 1:
    b = 1
    while b <= a:
        print(b, end="")
        b += 1
    print()
    a -= 1

enter image description here

and here is the result I get,I need to move the first loop to the right so I can get this shape enter image description here

After that I need to make the lower loop inverted and to use the number 1 from the top loop to make it seem like it fits with the lower loop one, I was able to do it with a for loop but i am not sure how I can do it with a while loop and maintain the shape.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2025-02-05 14:28:16

您绝对可以使用三角洲,只需在每个数字打印循环之前打印它即可获得间距。对于第二个循环,您可以每次将其减少2个,以便结束与第一个循环启动相同的位置。

i = 1
delta = 8
while i <= 5:
    j = 1
    print(delta*" ",end="")
    while j <= i:
        print(j, end=" ")
        j += 1
    print()
    i += 1
i = 2
while i <= 5:
    j = 1
    delta -= 2
    print(delta*" ",end="")
    while j <= i:
        print(j, end=" ")
        j += 1
    print()
    i += 1

编辑:这看起来像是一堂课的问题,所以我试图将代码保留在您的样式中

You can definitely use a delta, just print it before each number print loop to get the spacing. For the second loop you can decrement it by 2 each time so the ending stays in the same place as the first loop starts.

i = 1
delta = 8
while i <= 5:
    j = 1
    print(delta*" ",end="")
    while j <= i:
        print(j, end=" ")
        j += 1
    print()
    i += 1
i = 2
while i <= 5:
    j = 1
    delta -= 2
    print(delta*" ",end="")
    while j <= i:
        print(j, end=" ")
        j += 1
    print()
    i += 1

Edit: This is looking suspiciously like a question from a class so I tried to keep the code in what appears to be your style lol

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文