Home > Mobile >  Removing last line and initial whitespaces from an 8 by 8 list of integers in python
Removing last line and initial whitespaces from an 8 by 8 list of integers in python

Time:10-27

#Print the user board 

s = ''
for i in range(n):
    for j in range(n):
        z = i * n   j
        s  = ' '
        if z < 10:
            s  = ' '
        s  = str(z)
    s  = '\n'
print(dedent(s))

queens = list(map(int, input("Queens: ").split()))

I keep getting an error from my testcase environment of a last blank line before proceeding to the queens input below. What can I approach to fix

I have tried cleandoc from textwrap and while it works, it disrupts every required spacing and new distinctive lines for z from the "s" string which is a perfect 8 by 8 from 0 to 63.

CodePudding user response:

Creating the right way instead of removing things after is easier:

from itertools import count
import toolz

n = 8
start_value = 0
counter = count(start_value)
rows = [" ".join(f"{num:2}" for num in toolz.take(n, counter)) for _ in range(n)]
print("\n".join(rows))

CodePudding user response:

If you do not want the initial space, you'd better use join, which only adds the separator between consecutive items:

s = '\n'.join(' '.join('{:2d}'.format(i * n   j)
                       for j in range(n))
              for i in range(n))
print(s)

gives as expected:

 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
  • Related