0
0
Pythonprogramming~10 mins

For–else execution 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 "Found" if 3 is in the list, otherwise print "Not found".

Python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
    if num == [1]:
        print("Found")
        break
else:
    print("Not found")
Drag options to blanks, or click blank then click option'
A3
B4
C5
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number not in the list, so the else block always runs.
Forgetting to use break to stop the loop when found.
2fill in blank
medium

Complete the code to print "No even number" if no even number is found in the list.

Python
nums = [1, 3, 5, 7]
for n in nums:
    if n % [1] == 0:
        print("Even number found")
        break
else:
    print("No even number")
Drag options to blanks, or click blank then click option'
A3
B4
C5
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using a divisor other than 2, so the condition never matches.
Not using modulo operator correctly.
3fill in blank
hard

Fix the error in the code to correctly print "Found" if 0 is in the list, else "Not found".

Python
values = [1, 2, 3, 4]
for val in values:
    if val == [1]:
        print("Found")
        break
else:
    print("Not found")
Drag options to blanks, or click blank then click option'
A0
B4
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number that is actually in the list, which does not match the task.
Not understanding the else block runs only if break never happens.
4fill in blank
hard

Fill 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}
print(squares)
Drag options to blanks, or click blank then click option'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication (*) instead of exponentiation (**).
Using less than (<) instead of greater than (>) in the condition.
5fill in blank
hard

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

Python
result = [1]: [2] for k, v in data.items() if v [3] 1
print(result)
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.
Swapping keys and values.