+ 1

PLEASE help!!

You want to draw full and empty squares of a given size. A full square contains no empty space and an empty square contains no characters other than at its boundary. Write functions full_squares and empty_squres that take a string named Char and an integer named Size as parameters, and print the respective squres. Char is the character to be used to draw the boundary and to fill up the interior. Write a function squres that takes the same parameters and draws the corresponding full squares followed an empty square separated by a new line. I can’t solve the empty square bit. But I have solved the full square. Can someone please tell how do I the empty squares one? See my code in the comments

16th Sep 2018, 4:54 PM
Hanna Gatta
Hanna Gatta - avatar
14 Respostas
+ 6
Hanna Gatta Exactly! It's very common in many programming languages to start counting at 0. You're very welcome! Feel free to ask in the Q&A, the community here is pretty great đŸ‘đŸ»đŸ˜
16th Sep 2018, 8:04 PM
Eduardo Petry
Eduardo Petry - avatar
+ 3
def empty_squares(char, size): for i in range(size): if i == 0 or i == size-1: print(char * size) else: print(char + " "*(size-2) + char)
16th Sep 2018, 5:00 PM
Eduardo Petry
Eduardo Petry - avatar
+ 3
You have to call the function after defining it. # example: empty_squares("a", 5) Also: def squares(char, size): full_squares(char, size) print() empty_squares(char, size)
16th Sep 2018, 5:17 PM
Eduardo Petry
Eduardo Petry - avatar
+ 2
Well, `range(size)` is a sequence of integers starting at 0 and going up to size-1. When you do `for i in range(size)` you are making `i` get every value in that sequence, one at a time. The first value `i` gets is 0, and the last value `i` gets is size-1. That's why when `i == 0 or i == size-1` it means you are either in the first or the last iteration of your `for` loop, thus you are constructing either the first or the last line of the square.
16th Sep 2018, 7:49 PM
Eduardo Petry
Eduardo Petry - avatar
16th Sep 2018, 4:58 PM
Hanna Gatta
Hanna Gatta - avatar
+ 1
i tried that code, theres no ouput Eduardo Petry
16th Sep 2018, 5:16 PM
Hanna Gatta
Hanna Gatta - avatar
+ 1
I think it may look like this; * Based on Eduardo Petry code. def full_squares(char, size): for i in range(size): print(char * size) def empty_squares(char, size): pad = char + " " * (size - 2) + char for i in range(size): if i == 0 or i == size - 1: print(char * size) else: print(pad) def squares(char, size): full_squares(char, size) print() empty_squares(char, size) squares('*', 4)
16th Sep 2018, 5:21 PM
Ipang
+ 1
yall it doesng work! i dont really get how do u find out your formulas, but also the output form these arent quite right. i need 4 line of asteriks, each cointaing 4. the i need a box of asteriks as same as above, only now it is hollow in the middle.
16th Sep 2018, 6:09 PM
Hanna Gatta
Hanna Gatta - avatar
+ 1
wish i could have attched a pic but i dunno how
16th Sep 2018, 6:09 PM
Hanna Gatta
Hanna Gatta - avatar
+ 1
What is it that is not working? Running Ipang 's code gives this output: **** **** **** **** **** * * * * **** Isn't that the expected output? Are you getting an error when running the code?
16th Sep 2018, 6:28 PM
Eduardo Petry
Eduardo Petry - avatar
+ 1
oh okay, i get it now! but why did we put the size-2 thing? can u please explain the rationale behind the empty squares one? Thanks!
16th Sep 2018, 7:17 PM
Hanna Gatta
Hanna Gatta - avatar
+ 1
Hanna Gatta The lines of the empty squares are built following this logic: The first and last lines are `char * size` (they are the only "full" lines). Every other line starts with `char`, then has `size - 2` spaces and ends with `char`.
16th Sep 2018, 7:23 PM
Eduardo Petry
Eduardo Petry - avatar
+ 1
Got it!! but what about the i=0 and i= size-1? whats going on in this one? thanks
16th Sep 2018, 7:37 PM
Hanna Gatta
Hanna Gatta - avatar
+ 1
ohhh so size-1 is the last! bc phthon counts eveything one less. Got it! thank so very much, I really appreaciate you taking the time to explain me this 😇
16th Sep 2018, 8:00 PM
Hanna Gatta
Hanna Gatta - avatar