Coming up with a nested loop in Python

42 views Asked by At

i am a python beginner trying to come up with a nested loop but the output does not arrange the symbol in rows and columns but as a straight vertical column... Here's my code...help me fix it

rows = int(input("Enter the number of rows: "))
columns = int(input("Enter the number of columns: "))
symbol = input("Enter the symbol to use: ")

for i in range(rows):
   for j in range(columns):
      print(symbol)
   print()

i've asked around my classmates and no one seems to know the problem

1

There are 1 answers

2
SIGHUP On

You could call print() multiple (row * columns) times or just once per outer (rows) loop by building a string of the appropriate column length as follows:

rows = 10
columns = 15
symbol = "*"

for _ in range(rows):
    row = ""
    for _ in range(columns):
        row += symbol
    print(row)

Of course, you don't actually need a nested loop to achieve your output objective. You could just do this:

print(*[symbol * columns for _ in range(rows)], sep="\n")