0
0
Pythonprogramming~10 mins

While–else behavior 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 "Done" after the while loop finishes normally.

Python
count = 0
while count < 3:
    print(count)
    count += 1
else:
    print([1])
Drag options to blanks, or click blank then click option'
Acount
B"Done"
CDone
D"count"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string in print.
Trying to print the variable count instead of the message.
2fill in blank
medium

Complete the code to break the loop when count reaches 2.

Python
count = 0
while count < 5:
    if count == [1]:
        break
    print(count)
    count += 1
else:
    print("Finished")
Drag options to blanks, or click blank then click option'
A2
B4
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number outside the loop range.
Not breaking the loop at the correct count.
3fill in blank
hard

Fix the error in the else block to print "Loop ended" only if no break occurs.

Python
i = 0
while i < 4:
    if i == 5:
        break
    print(i)
    i += 1
else:
    print([1])
Drag options to blanks, or click blank then click option'
A"Loop ended"
BLoop ended
Ci
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Printing a variable instead of a string.
Missing quotes causing a NameError.
4fill in blank
hard

Fill both blanks to create a dictionary of squares for numbers less than 5, but only if the number is even.

Python
squares = {x: x[1]2 for x in range(5) if x [2] 2 == 0}
Drag options to blanks, or click blank then click option'
A**
B%
C==
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ** for squares.
Using == instead of % for checking evenness.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 1.

Python
result = { [1]: [2] for k, v in data.items() if v [3] 1 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() for keys.
Using < instead of > in the condition.