简体   繁体   English

使用 for 循环将标题添加到乘法表

[英]Adding Headers to a Multiplication Table using for-loops

I am making a product table that the user gives an input to determine its size.我正在制作一个产品表,用户提供输入以确定其大小。

The columns are labeled AZ The rows are labeled by Numbers with a "|"列标记为 AZ 行标记为带有“|”的数字next to it.在它的旁边。

The program prints all headers correctly.该程序正确打印所有标题。 The only issue is, the product table is printed the same amount of times as the input along the rows.唯一的问题是,产品表的打印次数与沿行输入的次数相同。

See attached image for my attempt with an input of '3'.请参阅附加图像以了解我输入“3”的尝试。 The black product table on the image with an input of '4' is how the answer should be formatted.图像上输入为“4”的黑色产品表是答案的格式。

def get_pro_tab():
        n = int(input('Please enter a positive integer: '))
        print("(Don't go too crazy)")
        print()

        #letters
        print('  ',end='')
        for letter in range(n):
                print('{0:4}'.format(chr(65 + letter)),end=' ')
        print()
        #margins
        marigin = 0
        for number in range(1, n+1):
                print(str(number)+ "|", end='')
        #numbers
                prod = 0
                for y in range(1, n+1):
                        for z in range(1, n+1):
                                prod = y * z
                                print('{0:4}'.format(str(prod)), end=' ')
                        print('\n')

Attempted output and answer if the input was 4如果输入为 4,则尝试输出并回答

I think you just had one extra loop in your code.我认为您的代码中只有一个额外的循环。 I just commented it out, now I think it works:我刚刚评论了它,现在我认为它有效:

def get_pro_tab():
    n = int(input('Please enter a positive integer: '))
    print("(Don't go too crazy)")
    print()

    #letters
    print('  ',end='')
    for letter in range(n):
        print('{0:4}'.format(chr(65 + letter)),end=' ')
    print()
    #margins
    marigin = 0
    for number in range(1, n+1):
        print(str(number)+ "|", end='')
    #numbers
        prod = 0
        #for y in range(1, n+1):
        for z in range(1, n+1):
            prod = number * z
            print('{0:4}'.format(str(prod)), end=' ')
        print()

For input 5 this prints:对于输入 5,这会打印:

  A    B    C    D    E    
1|1    2    3    4    5    
2|2    4    6    8    10   
3|3    6    9    12   15   
4|4    8    12   16   20   
5|5    10   15   20   25   

If you like, you can make your life even a bit easier by using the format strings like:如果您愿意,可以使用如下格式字符串让您的生活更轻松:

def get_pro_tab():
    n = int(input('Please enter a positive integer: '))
    print("(Don't go too crazy)")
    print()

    #letters
    print('  ',end='')
    for letter in range(n):
        print('{0:>4s}'.format(chr(65 + letter)),end=' ')
    print()
    #margins
    marigin = 0
    for number in range(1, n+1):
        print(str(number)+ "|", end='')
    #numbers
        prod = 0
        #for y in range(1, n+1):
        for z in range(1, n+1):
            prod = number * z
            print('{0:4d}'.format(prod), end=' ')
        print()

If you run this, with the same input, you get:如果您使用相同的输入运行它,您将得到:

     A    B    C    D    E 
1|   1    2    3    4    5 
2|   2    4    6    8   10 
3|   3    6    9   12   15 
4|   4    8   12   16   20 
5|   5   10   15   20   25 

Ok, I know, beauty lies in the eye of the beholder, but to me it looks more appealing if the numbers are aligned to the right.好吧,我知道,美丽在于旁观者的眼睛,但对我来说,如果数字向右对齐,它看起来更有吸引力。 But again it was just a very small modification of your code.但同样只是对您的代码进行了很小的修改。

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

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