三角形模式打印在Python中

huangapple go评论71阅读模式
英文:

Triangle pattern printing in Python

问题

max = 37
max2 = max

no_of_lines = 0
subtractor = 1
while max >= 1:
    no_of_lines += 1
    max = max - subtractor
    subtractor += 1

counter = 1
for i in range(no_of_lines):
    print((no_of_lines - i) * " " + (no_of_lines - i - 1) * " ", end="")

    # the next loop is to create the string of numbers in a line
    s = ""
    for k in range(i + 1):
        if (counter > max2):
            s = " *" + s
        else:
            s = " " + str(counter) + s
        counter += 1
    print(s)
英文:

Write a program that prompts the user to enter the maximum value of range and display the values in the triangle format and use any notation for the white space to make a complete triangle.Example:Enter the maximum range: 37
三角形模式打印在Python中

This is the solution I came up with. But the space printed is incorrect. Can anyone help?

max = 37
max2 = max

no_of_lines = 0
subtractor = 1
while max>=1:
    no_of_lines+=1
    max = max-subtractor
    subtractor +=1

counter = 1
for i in range(no_of_lines):
    print((no_of_lines-i)*" " + (no_of_lines-i-1)*" ", end="")

    # the next loop is to create the string of numbers in a line
    s = ""
    for k in range(i+1):
        if(counter>max2):
            s = " "+"*"+s
        else:
            s = " "+str(counter)+s
        counter+=1
    print(s)

答案1

得分: 2

确保任何N值(在OP的原始代码中被误称为max)的正确对齐都很麻烦。问题中有缺陷的代码暗示N将始终是两位数,但这可能并非一定如此。

尽管更复杂,但这种方法适用于多位数的N:

N = 37
v = i = 1
t = []

while v <= N:
    t.append([])
    for _ in range(i):
        t[-1].append(v)
        if (v := v + 1) > N:
            break
    i += 1

m = 1 if N == 1 else len(t[-2])+1
w = len(str(N))

for i, _t in enumerate(t, 1):
    line = [f'{v:>{w}}' for v in _t]
    if (pad := m - len(line)) > 0:
        fill = '*' if i == len(t) else ' '
        line += [f'{fill:>{w}}' for _ in range(pad)]
    print(' '.join(reversed(line)))

输出:

                             1
                          3  2
                       6  5  4
                   10  9  8  7
                15 14 13 12 11
             21 20 19 18 17 16
          28 27 26 25 24 23 22
       36 35 34 33 32 31 30 29
     *  *  *  *  *  *  *  * 37
英文:

Ensuring proper alignment for any value of N (spuriously named max in OP's original code) is awkward. The flawed code in the question implies that N will always be a 2-digit value but that may not necessarily be the case.

This approach, albeit more complex, handles multi-digit values for N:

N = 37
v = i = 1
t = []

while v &lt;= N:
    t.append([])
    for _ in range(i):
        t[-1].append(v)
        if (v := v + 1) &gt; N:
            break
    i += 1

m = 1 if N == 1 else len(t[-2])+1
w = len(str(N))

for i, _t in enumerate(t, 1):
    line = [f&#39;{v:&gt;{w}}&#39; for v in _t]
    if (pad := m - len(line)) &gt; 0:
        fill = &#39;*&#39; if i == len(t) else &#39; &#39;
        line += [f&#39;{fill:&gt;{w}}&#39; for _ in range(pad)]
    print(&#39; &#39;.join(reversed(line)))

Output:

                         1
                      3  2
                   6  5  4
               10  9  8  7
            15 14 13 12 11
         21 20 19 18 17 16
      28 27 26 25 24 23 22
   36 35 34 33 32 31 30 29
 *  *  *  *  *  *  *  * 37

答案2

得分: 1

这是使您的代码运行所需的调整。顺便说一下,查看rjust函数的建议很好。

max = 37
max2 = max

no_of_lines = 0
subtractor = 1
while max >= 1:
    no_of_lines += 1
    max = max - subtractor
    subtractor += 1

counter = 1
for i in range(no_of_lines):
    print((no_of_lines - i) * " " + (no_of_lines - i - 1) * "  ", end="")

    # 下一个循环是创建单行数字字符串
    s = ""
    for k in range(i + 1):
        if counter > max2:
            s = " " + " *" + s
        else:
            s = " " + "%2d" % counter + s
        counter += 1
    print(s)

输出:

                           1
                        3  2
                     6  5  4
                 10  9  8  7
              15 14 13 12 11
           21 20 19 18 17 16
        28 27 26 25 24 23 22
     36 35 34 33 32 31 30 29
   *  *  *  *  *  *  *  * 37
英文:

Here are the tweaks needed to make your code work. BTW, the advice to look at the rjust function is good advice.

max = 37
max2 = max

no_of_lines = 0
subtractor = 1
while max&gt;=1:
    no_of_lines+=1
    max = max-subtractor
    subtractor +=1

counter = 1
for i in range(no_of_lines):
    print((no_of_lines-i)*&quot; &quot; + (no_of_lines-i-1)*&quot;  &quot;, end=&quot;&quot;)

    # the next loop is to create the string of numbers in a line
    s = &quot;&quot;
    for k in range(i+1):
        if(counter&gt;max2):
            s = &quot; &quot;+&quot; *&quot;+s
        else:
            s = &quot; &quot;+&quot;%2d&quot;%counter+s
        counter+=1
    print(s)

Output:

                           1
                        3  2
                     6  5  4
                 10  9  8  7
              15 14 13 12 11
           21 20 19 18 17 16
        28 27 26 25 24 23 22
     36 35 34 33 32 31 30 29
   *  *  *  *  *  *  *  * 37

huangapple
  • 本文由 发表于 2023年5月29日 02:06:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定