0
0
Pythonprogramming~10 mins

Nested for loop execution 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 for loop.

Python
for i in [1]:
    print(i)
Drag options to blanks, or click blank then click option'
A[1, 2]
Blist(3)
C3
Drange(1, 4)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single number like 3 instead of a range.
Using a list with fewer numbers than needed.
2fill in blank
medium

Complete the code to print all pairs of numbers from 1 to 2 using nested loops.

Python
for i in range(1, 3):
    for j in [1]:
        print(i, j)
Drag options to blanks, or click blank then click option'
A[1, 2, 3]
Brange(0, 2)
Crange(1, 3)
Drange(3, 5)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range that starts at 0 or goes beyond 2.
Using a list with incorrect numbers.
3fill in blank
hard

Fix the error in the nested loop to print all pairs of letters and numbers.

Python
letters = ['a', 'b']
numbers = [1, 2]
for letter in letters:
    for num in [1]:
        print(letter, num)
Drag options to blanks, or click blank then click option'
Arange(1, 3)
Bnumbers
Cletters
Dletter
Attempts:
3 left
💡 Hint
Common Mistakes
Using the outer loop variable letter inside the inner loop.
Using the wrong list like letters again.
4fill in blank
hard

Fill both blanks to create a nested loop that prints all combinations of colors and sizes.

Python
colors = ['red', 'blue']
sizes = ['S', 'M']
for [1] in colors:
    for [2] in sizes:
        print(color, size)
Drag options to blanks, or click blank then click option'
Acolor
Bsize
Citem
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both loops.
Using generic names like item or value that don't match the print statement.
5fill in blank
hard

Fill all three blanks to create a nested loop that builds a dictionary with keys as colors and values as sizes.

Python
colors = ['red', 'green']
sizes = ['L', 'XL']
combo_dict = {}
for [1] in colors:
    for [2] in sizes:
        combo_dict[[3]] = size
print(combo_dict)
Drag options to blanks, or click blank then click option'
Acolor
Bsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable as dictionary key.
Mixing up variable names in the assignment.