-3

So I just started learning python and I am trying to make a rectangle out of the same recurring character, for example if height=5 and width=3, and the character is 'P,' a 5x3 rectangle of P's would be created:

PPPPP
PPPPP
PPPPP

But how do I write a program that does it. How would I go about doing this? I know I have to use "for" loops but I'm not exactly sure what I have to do. Thanks The code I have so far is:

def width_of_rectangle(width_size):
  for current_length in range(width_size):
    print('P',end='')

def height_of_rectangle(height_size):
  for current_height in range(height_size):
    width_of_rectangle(current_lenght)
    print('P',end='')

But it is making a triangle instead of a rectangle because it is not repeated the same number of times, it comes out as:

P
PP
PPP
PPPP
PPPPP

I know I'm doing something wrong but not sure what it is

SirParselot
  • 2,640
  • 2
  • 20
  • 31
  • 3
    Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – Morgan Thrapp Oct 05 '15 at 17:54
  • 1
    You don't need for loops to do this, strictly speaking. Hint: `"*" * 5` is a valid expression. – Kevin Oct 05 '15 at 17:55
  • I would suggest reading through this http://www.tutorialspoint.com/python/ – SirParselot Oct 05 '15 at 17:55
  • Okay, but I'm trying to learn how to use loops and was seeing how I could do it using them – Rodrigo Cardenas Oct 05 '15 at 17:57
  • 1
    Try using two for loops, if you still can't get it then post what you've tried to show people you've tried something – SirParselot Oct 05 '15 at 18:00
  • Just as a side note, this is how your question should have been presented from the start. It asks a question and it also has code to show that you've tried something. – SirParselot Oct 05 '15 at 19:00

2 Answers2

1

Since you don't seem to grasp the concept of how to use a for loop I'll try to explain it. For loops execute whatever code is inside of them x number of times. They are used when you know how many times you want to iterate over your code. To specify a the number of times you would use the range() function if you have python 3.3 or xrange() if you have 2.7. In either case they work the same. You can specify a starting point, ending point, and a skip.

>>>range(5, 11, 2)
5
7
9

Notice how 11 isn't in the range? That is because range goes up to the number before the end point which is 10. You can leave the skip blank since it is 1 by default (range(5,11)). Now to use that in a for loop you would write it like so

>>>for x in range(5,11,2):
...    print(x)

for each iteration of the loop x is the current value of the range.


You can also nest loops which is a loop inside of a loop. If we have

>>>for i in range(5):
...    for j in range(4):
...       print('*')

The inner for loop will be executed 5 times and the print statement will be executed 20 times. Hopefully that will get on the right track to figuring out your problem.

This link will probably be useful since print automatically adds a newline character onto whatever you are printing.

Community
  • 1
  • 1
SirParselot
  • 2,640
  • 2
  • 20
  • 31
  • Okay, so far I am successfully making a triangle but I'm confused as to how to make it a rectangle instead. What I have is: `def width_of_rectangle(width_size): for current_length in range(width_size): print('*',end='') def height_of_rectangle(height_size): for current_height in range(1,height_size+1): width_of_rectangle(current_height) print()` Which makes a triangle.(I have no idea how to edit this) – Rodrigo Cardenas Oct 05 '15 at 18:42
  • @RodrigoCardenas your problem is that you are calling your `width_of_rectangle` function with `current_height`. What is happening is that the first iteration of your loop the `current_height` is `1` so for current length it will only loop once. For the second iteration `current_height` is `2` so the `current_length` loop will iterate twice. See if you can figure it out from that. – SirParselot Oct 05 '15 at 18:54
1

Using for, and following SirParselot's great write-up, will likely be the better option for readability, however for loops aren't your only option!

def make_char_rectangle(char, height, width):
    return ((char*width)+'\n')*height

Breaking it apart a little, we get:

def make_char_rectangle(char, height, width):
    line = char * width
    rectangle = (line + '\n') * height
    return rectangle

With either of the above:

>>> print(make_char_rectangle('L', 3, 5))
LLLLL
LLLLL
LLLLL
Rejected
  • 4,445
  • 2
  • 25
  • 42