Search code examples
pythonfunctionfor-loopbox

Printing a filled box with Python


my beginner brain is again unable to get this to work. I need to print a solid box where the user determines height, width, and what character is used in the print. How should I define the range in the for loop? Clearly not as I have done here:

def print_box(w, h, m):
    """print filled box"""
    for i in range(w):
        for j in range(h):
            print(m, end = "")
        
def main():
    w = input("Enter the width of a frame: ", )
    h = input("Enter the height of a frame: ", )
    m = input("Enter a print mark: ")

    print()
    print_box(w, h, m)

Solution

  • You have your loops backwards. The outer loop should be height, inner loop should be width.

    You also need to print a newline after each line.

    You don't really need the inner loop, since you can multiply a string to repeat it.

    def print_box(w, h, m):
        """print filled box"""
        for _ in range(h):
            print(m * w)