2

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 = ''.

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

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

def main():
    for i in range(100):
        getPentagonalNumber(i)
1
  • 1
    Can you just do 2 for-loops, with the inner loop from 1 to 10? Commented Feb 12, 2014 at 12:40

3 Answers 3

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()

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

>>> 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.

>>> ['%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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks mate, But what does the '%6d' do?
@user3268003 %6d is format string to print an integer as string. d stands for decimal and 6 stands for in width of 6. Try on your console >>> "my age is %d, I study in %d standard" % (10, 14)
@JayanthKoushik I mean 10 class and 14 age, wrong values supplied :D
2
for i in range(100):
    print(getPentagonalNumber(i), end=' ')
    if i % 10 == 9:
        print('\n') 

Comments

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

3 Comments

I've simplified your code a bit; however the logic still isn't quite right (you have 11 numbers on the first line and 9 on the last)... perhaps better use if not (i+1)%10:
There are 9 on the last because the loop doesnt include 100
Looked into it and fixed it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.