-1

I know there are numerous way to do this but I am trying to print a rectangle using ranges and if else statements. My code is below works but only cause I have hard coded the spaces required in print statement at line 7. As soon as the values for num_rows or num_cols changes the spacing is off. What do I need to do to make it work?

num_rows = 5
num_cols = 6

for i in range(num_rows):
    print('*', end=' ')
    if i in range(1,num_rows -1):
        print('       ','*', end='')
else:
    for j in range(num_cols-1):
        print('*', end=' ')
print('')

I got the code to turn into the code below after tinkering but it still is not the right and doesn't work once I change the values for the row and cols variables. I am not sure what I need to do to my size variable to be flexible and work with any values for num_rows and num_cols. Is it possible to make this work using only what I have covered so far in my class which is basically whats above lol. I have asked my teacher for help and he stated that I should try searching forums for help first. He stated that most developers do that everyday so I need to get used to asking help.

num_rows = 5
num_cols = 6
size =(num_rows  + 3)

for i in range(num_rows):
    print('*', end=' ')
    if i in range(1,num_rows -1):
        print( ' ' * size + '*', end='')
else:
    for j in range(num_cols-1):
        print('*', end=' ')
print('')
negru08
  • 5
  • 1
  • 4
  • The only thing different between those two snippets is that in the second one you are inserting `size` spaces, correct? Can you explain the logic behind using `size =(num_rows + 3)`? – 0x5453 Mar 15 '18 at 18:47
  • I the size variable cause without it the output is not correct. And yes the difference between the two iterations of code is on the second I decided to use a variable instead of just hard coding spaces. – negru08 Mar 15 '18 at 18:54
  • Your posted code prints everything on a single line; even the first code block doesn't print a rectangle. – Prune Mar 15 '18 at 18:59
  • What do you mean? From my understanding prints everything on the first line and goes through the loop till it gets to the last print statement that starts a new line. – negru08 Mar 15 '18 at 19:02
  • @negru08 I was specifically asking about the `num_rows + 3` calculation. Because if you think about the logic there, that won't give you the correct amount of whitespace inside your rectangle. – 0x5453 Mar 15 '18 at 20:27

1 Answers1

1

Python makes it easier to do more with less code.

You could make the rectangle like

print("* "*num_cols, end='')
print(("\n* "+"  "*(num_cols-2)+"* ")*(num_rows-2))
print("* "*num_cols)

Line 1 prints num_cols *s without a newline after that.
Line 2 prints num_rows-2 lines where each line consists of a * at each end with num_cols-2 number of (spaces) in between.
Line 3 prints the bottom of the rectangle.

Your program should work fine if the number of spaces printed is changed from

print( ' ' * size + '*', end='')

to

print( '  ' * (num_cols-2)+ '*', end='')

Two spaces are needed because you have end=' ' in print()s before it. If their end is made end='', one space would suffice.

J...S
  • 5,079
  • 1
  • 20
  • 35
  • While I agree there is a simpler way of doing this code . I am challenged to try and make this working using loop statements that involve range . The actual code started as this below and I had to modify it to print a hollow rectangle : num_rows = 2 num_cols = 3 for i in range(num_rows): print('*', end=' ') for j in range(num_cols-1): print('*', end=' ') print('') – negru08 Mar 15 '18 at 18:58
  • Thank you, I got it to work. Some how I had messed up my loop when I pasted it here. But after I realligned everything and used your statement it works!! Thank you. Can you explain why the num_cols -2 is needed or how it works? – negru08 Mar 15 '18 at 19:18
  • A `*` is printed on either ends of each line (other than the top-most and bottom-most lines) and there are `num_cols` columns. There are `num_cols-2` columns left to take care of. – J...S Mar 15 '18 at 19:26