简体   繁体   English

我无法让我的代码在 Python 中绘制带有嵌套循环的数字模式,这将以整齐的方式对齐所有数字打印输出

[英]I cant get my code to draw a number pattern with nested loops in Python that will align all number printouts in a neat manner

Use nested for loops to create the following printout:使用嵌套的 for 循环创建以下打印输出:

在此处输入图像描述

The number of rows should be read from the user.应该从用户那里读取行数。 Use formatted printouts so that the numbers are aligned even for two-digit numbers.使用格式化的打印输出,以便即使是两位数的数字也能对齐。 • All printed numbers correspond to column numbers. • 所有打印的数字对应于列号。 • No number is printed if the column number is less than the row number. • 如果列号小于行号,则不打印数字。 • Print a suitable number of spaces to fill an empty column. • 打印适当数量的空格以填充空列。

# Program to print a pattern with numbers

print("Program to print a pattern with numbers ")
print("-" * 50)
n = int(input("Please enter the number of rows "))
print("-" * 50)

for i in range(n + 1):
    # Increase triangle hidden
    for j in range(i):
        print(" ", end=' ')
    #  Decrease triangle to include numbers in pattern not in increment
    for j in range(i, n):
        print(j + 1, end=" ")
    print()

The code above produces the required output but the numbers are not aligned in an input with 2 digits.上面的代码生成所需的 output,但数字在 2 位输入中未对齐。 How do I format the iterables to make a perfectly aligned output printout.我如何格式化 iterables 以制作完美对齐的 output 打印输出。

Output: Output:在此处输入图像描述

You can use str.rjust to right-justify a string.您可以使用str.rjust将字符串右对齐。 As long as you know the overall length of each line, that makes it easy to align each line so that they're all aligned on the right:只要您知道每条线的总长度,就可以轻松对齐每条线,使它们都在右侧对齐:

>>> n = 5
>>> for i in range(1, n+1):
...     print(''.join(str(j).rjust(3) for j in range(i, n+1)).rjust(n*3))
...
  1  2  3  4  5
     2  3  4  5
        3  4  5
           4  5
              5

This is how you might modify your code to use str.rjust .这就是您可以修改代码以使用str.rjust的方式。 Adjust the 2 in rjust(2) to whatever number you want.rjust(2)中的2调整为您想要的任何数字。

print("Program to print a pattern with numbers ")
print("-" * 50)
n = int(input("Please enter the number of rows "))
print("-" * 50)

for i in range(n + 1):
    # Increase triangle hidden
    for j in range(i):
        print(" ".rjust(2), end="  ")
    #  Decrease triangle to include numbers in pattern not in increment
    for j in range(i, n):
        print(str(j+1).rjust(2), end="  ")
    print()

For your example, this gives:对于您的示例,这给出了:

Program to print a pattern with numbers 
--------------------------------------------------
Please enter the number of rows 12
--------------------------------------------------
 1   2   3   4   5   6   7   8   9  10  11  12  
     2   3   4   5   6   7   8   9  10  11  12  
         3   4   5   6   7   8   9  10  11  12  
             4   5   6   7   8   9  10  11  12  
                 5   6   7   8   9  10  11  12  
                     6   7   8   9  10  11  12  
                         7   8   9  10  11  12  
                             8   9  10  11  12  
                                 9  10  11  12  
                                    10  11  12  
                                        11  12  
                                            12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM