在循环中的同一行上打印多个字符串

时间:2021-01-06 20:20:43

I read a few related articles with similar titles as mine but most of them just give basic syntax advice such as to use end = ''.

我阅读了一些与我的标题相似的相关文章,但大多数只提供基本的语法建议,例如使用end =''。

I am trying to display the pentagonal numbers with n being 1-100, and only displaying 10 numbers per line. I can't think of a way to do this without messing up my for statement. I could use end = '' but I don't know how to tell the loop 'okay after 10 iterations THEN skip to the next line

我试图显示五边形数字,n为1-100,每行只显示10个数字。如果不搞砸我的陈述,我想不出办法做到这一点。我可以使用end =''但是我不知道如何告诉循环'在10次迭代后没问题然后跳到下一行

def getPentagonalNumber(n):
    (n * ((3 * n) - 1))/2

def main():
    for i in range(100):
        getPentagonalNumber(i)

3 个解决方案

#1


3  

You can use join something like:

您可以使用以下连接:

for i in range(1, 100, 10): # i = 1, 11, 21, 31, 41,.....
  print(' '.join(['%6d' % getPentagonalNumber(n) for n in range(i, i+10)]))

Output:

输出:

     1      5     12     22     35     51     70     92    117    145
   176    210    247    287    330    376    425    477    532    590
   651    715    782    852    925   1001   1080   1162   1247   1335
  1426   1520   1617   1717   1820   1926   2035   2147   2262   2380
  2501   2625   2752   2882   3015   3151   3290   3432   3577   3725
  3876   4030   4187   4347   4510   4676   4845   5017   5192   5370
  5551   5735   5922   6112   6305   6501   6700   6902   7107   7315
  7526   7740   7957   8177   8400   8626   8855   9087   9322   9560
  9801  10045  10292  10542  10795  11051  11310  11572  11837  12105
 12376  12650  12927  13207  13490  13776  14065  14357  14652  14950

by the way you are missing return in function getPentagonalNumber()

顺便提一下你在函数中返回getPentagonalNumber()

What I am doing is generating numbers in gaps 10 in outer loop using range:

我正在做的是使用范围在外循环中的间隙10中生成数字:

>>> range(1, 100, 10)
[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]

Inside for loop create a list of numbers within gap (actually creates a list of number strings) e.g.

Inside for循环在gap中创建一个数字列表(实际上创建一个数字字符串列表),例如

>>> ['%3d' % getPentagonalNumber(n) for n in range(1, 1 +10)]
['  1', '  5', ' 12', ' 22', ' 35', ' 51', ' 70', ' 92', '117', '145']

and join number-strings as a single string and prints.

并将数字字符串作为单个字符串连接并打印。

#2


2  

for i in range(100):
    print(getPentagonalNumber(i), end=' ')
    if i % 10 == 9:
        print('\n') 

#3


2  

for i in range(100):
    if i % 10 == 9:
        print getPentagonalNumber(i)
    else:
        print getPentagonalNumber(i),

Tested this printing just i and got

我刚刚测试了这个印刷品

0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99

#1


3  

You can use join something like:

您可以使用以下连接:

for i in range(1, 100, 10): # i = 1, 11, 21, 31, 41,.....
  print(' '.join(['%6d' % getPentagonalNumber(n) for n in range(i, i+10)]))

Output:

输出:

     1      5     12     22     35     51     70     92    117    145
   176    210    247    287    330    376    425    477    532    590
   651    715    782    852    925   1001   1080   1162   1247   1335
  1426   1520   1617   1717   1820   1926   2035   2147   2262   2380
  2501   2625   2752   2882   3015   3151   3290   3432   3577   3725
  3876   4030   4187   4347   4510   4676   4845   5017   5192   5370
  5551   5735   5922   6112   6305   6501   6700   6902   7107   7315
  7526   7740   7957   8177   8400   8626   8855   9087   9322   9560
  9801  10045  10292  10542  10795  11051  11310  11572  11837  12105
 12376  12650  12927  13207  13490  13776  14065  14357  14652  14950

by the way you are missing return in function getPentagonalNumber()

顺便提一下你在函数中返回getPentagonalNumber()

What I am doing is generating numbers in gaps 10 in outer loop using range:

我正在做的是使用范围在外循环中的间隙10中生成数字:

>>> range(1, 100, 10)
[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]

Inside for loop create a list of numbers within gap (actually creates a list of number strings) e.g.

Inside for循环在gap中创建一个数字列表(实际上创建一个数字字符串列表),例如

>>> ['%3d' % getPentagonalNumber(n) for n in range(1, 1 +10)]
['  1', '  5', ' 12', ' 22', ' 35', ' 51', ' 70', ' 92', '117', '145']

and join number-strings as a single string and prints.

并将数字字符串作为单个字符串连接并打印。

#2


2  

for i in range(100):
    print(getPentagonalNumber(i), end=' ')
    if i % 10 == 9:
        print('\n') 

#3


2  

for i in range(100):
    if i % 10 == 9:
        print getPentagonalNumber(i)
    else:
        print getPentagonalNumber(i),

Tested this printing just i and got

我刚刚测试了这个印刷品

0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99