Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading