I saw my same question asked but I want to know why what I'm trying isn't working. This is from a Zybook challenge question.
Here is the exercise:
Write nested loops to print a rectangle. Sample output for given program:
* * *
* * *
This is the code I built:
num_rows = 2
num_cols = 3
for num_rows in range(0,num_rows):
for num_cols in range(0,num_cols):
print('*', end=' ')
print('')
The output is:
* * *
* *
Question: Why doesn't the nested for loop print statement iterate for the third time? When I set the nested loop to:
for num_cols in range(0,3):
I receive my desired output of the 3x2 asterisk rectangle. If the num_cols variable is declared as 3, shouldn't the output statement equal my desired output?
num_rowsandnum_colsfor the previously befined variables and the ones used in theforloop. Rename the latest ones to something likerowandcolinstead ofnum_rowsandnum_cols.num_rowsandnum_colsin the loop.