0
0
Pythonprogramming~10 mins

Nested while loops in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 3 using a while loop.

Python
i = 1
while i [1] 3:
    print(i)
    i += 1
Drag options to blanks, or click blank then click option'
A==
B>
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of a comparison operator for the loop condition.
Using '>' which would never start the loop since i=1.
2fill in blank
medium

Complete the code to print a 3x3 grid of stars using nested while loops.

Python
row = 0
while row < 3:
    col = 0
    while col [1] 3:
        print('*', end='')
        col += 1
    print()
    row += 1
Drag options to blanks, or click blank then click option'
A>
B<=
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes 4 stars per row.
Using '==' only runs the loop once.
3fill in blank
hard

Fix the error in the nested while loops to print numbers 1 to 3 in each row for 3 rows.

Python
row = 1
while row <= 3:
    col = 1
    while col [1] 3:
        print(col, end=' ')
        col += 1
    print()
    row += 1
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes the number 3 from printing.
Using '==' only runs the loop once.
4fill in blank
hard

Fill both blanks to create a nested while loop that prints a triangle of stars with 4 rows.

Python
row = 1
while row [1] 4:
    col = 1
    while col [2] row:
        print('*', end='')
        col += 1
    print()
    row += 1
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' for outer loop causes only 3 rows.
Using '>=' or '>' causes loops not to run as expected.
5fill in blank
hard

Fill all three blanks to create nested while loops that print a 3x3 grid of numbers counting from 1 to 9.

Python
num = 1
row = 1
while row [1] 3:
    col = 1
    while col [2] 3:
        print(num, end=' ')
        num [3] 1
        col += 1
    print()
    row += 1
Drag options to blanks, or click blank then click option'
A<=
B<
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' for outer loop excludes last row.
Using '-=' decreases the number instead of increasing.
Using '>=' or '>' causes loops not to run.