Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print numbers from 1 to 5 using a loop.
Python
for i in range(1, [1]): print(i)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 as stop will print 1 to 4 only.
✗ Incorrect
The range function stops before the end number, so to print 1 to 5, we use 6 as the stop.
2fill in blank
mediumComplete the code to stop the loop when i equals 3.
Python
for i in range(1, 6): if i == [1]: break print(i)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using break with wrong value stops loop too early or too late.
✗ Incorrect
The break stops the loop when i is 3, so numbers 1 and 2 print only.
3fill in blank
hardFix the error in the loop to skip printing number 3.
Python
for i in range(1, 6): if i == [1]: continue print(i)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue with wrong value skips wrong number.
✗ Incorrect
The continue skips the current loop when i is 3, so 3 is not printed.
4fill in blank
hardFill both blanks to create a dictionary of squares for numbers greater than 2.
Python
squares = {x: x[1]2 for x in range(1, 6) if x [2] 2} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ** changes meaning.
Using < instead of > filters wrong numbers.
✗ Incorrect
Use ** to square numbers and > to filter numbers greater than 2.
5fill in blank
hardFill all three blanks to create a dictionary with uppercase keys and values greater than 0.
Python
result = [1]: [2] for k, v in data.items() if v [3] 0}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() keeps keys lowercase.
Using < instead of > filters wrong values.
✗ Incorrect
Keys are converted to uppercase with k.upper(), values are v, and filter keeps values greater than 0.