Reputation: 13
I am trying to print N-queen for every case for given input n. For this I have to write the condition explicitly for j=n-1 to get a new line after every 'n' columns.
for i in range(n):
for j in range(n):
if j!=n-1:
if j==a[i]-1:
print('Q',end=' ')
else:
print('-',end=' ')
else:
if j==a[i]-1:
print('Q')
else:
print('-')
Output:
#One case in n=5
- - Q - -
Q - - - -
- - - Q -
- Q - - -
- - - - Q
For not using condition explicitly I get the output in one line, which is obvious.
- - Q - - Q - - - - - - - Q - - Q - - - - - - - Q
Is there any way to implicitly write condition in print function in the place of end=' '?
for i in range(n):
for j in range(n):
if j==a[i]-1:
print('Q',(end=' ' if j!=n-1)) #this is not correct
else:
print('-',if j!=n-1: end=' ' ) #also not correct
I am using IDLE for python 3.4.2.
Upvotes: 0
Views: 7113
Reputation: 798516
Just print the newline at the end of the outer loop.
for i in range(n):
for j in range(n):
...
print()
Upvotes: 2