0
from sys import argv  

char = argv[1]   
n = int(argv [2])      

for i in range(0, n):        
       if i == 0 or n - 1:   
         for j in range(0, n): 
            char += "n" 
            print char * n 
       else: 
         for j in range(0, n): 
            if j == 0 or j = n-1: 
                char += "n" 
            else: 
    char += "/n" 
Taku
  • 31,927
  • 11
  • 74
  • 85

2 Answers2

1

I suppose you can do something like this:

char = 'n'
n = 10
# char = argv[1]  
# n = int(argv [2])  

print n*char
for i in range(n-2):
    print (' '*(n-2)).center(n, char)
print n*char

# output: a 10 x 10 "square" of n
nnnnnnnnnn
n        n
n        n
n        n
n        n
n        n
n        n
n        n
n        n
nnnnnnnnnn
Taku
  • 31,927
  • 11
  • 74
  • 85
0

abccd's answer should work well. But if you are wondering why your code isn't working:

  1. The if condition if i == 0 or n - 1 will be true for all n>1. Probably, you wanted to write if i == 0 or i == n - 1

  2. You don't really need the for j... loop within the if i == 0 or i == n - 1. Doing a print char * n prints the char n times.

  3. The if j == 0 or j = n-1: again is not correct. j = n-1 part does the assignment instead of comparison.

  4. To print the char or spaces in the if-else you can use the sys.stdout.write.

The final code could look like:

from sys import argv  
import sys

char = argv[1]  
n = int(argv [2])  

for i in range(0, n): 

    if i == 0 or i == n - 1: 
         print char * n 
    else: 
        for j in range(0, n): 
            if j == 0 or j == n-1: 
                sys.stdout.write(char)
            else:
                sys.stdout.write(' ')
        sys.stdout.write("\n")

And work like so

$ python script.py q 5
qqqqq
q   q
q   q
q   q
qqqqq
yeniv
  • 1,589
  • 11
  • 25
  • this is actually working now, and this is what I intended to see on my command prompt screen. I just want to ask what the "sys.stdout.write" do? – Alger Aranda May 28 '17 at 05:42
  • @AlgerAranda, The reason I used `sys.stdout.write` here is that it just prints the given character(s) to console and (as opposed to `print`) does not add the newline character. You may want to check this for a details: https://stackoverflow.com/a/3263763/4045754 – yeniv May 28 '17 at 16:42