Search code examples
pythonnested-loops

Python Nested Loops To Print Rectangle With Asterisks


Write nested loops to print a rectangle. Sample output for given program:

3 stars : ***

3 stars : ***

I tried it and ended up with this:

num_rows = 2

num_cols = 3

'''IDK WHAT TO PUT HERE'''
    print('*', end=' ')
print('')

Any help would be appreciated! Thanks!


Solution

  • Here you go! Try this!

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