0
0
Pythonprogramming~10 mins

Assert statement usage 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 check if the variable age is at least 18 using an assert statement.

Python
age = 20
assert [1], "Age must be at least 18"
Drag options to blanks, or click blank then click option'
Aage > 18
Bage >= 18
Cage = 18
Dage < 18
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment (=) instead of comparison (>=).
Checking if age is less than 18 instead of greater or equal.
2fill in blank
medium

Complete the code to assert that the list numbers is not empty.

Python
numbers = [1, 2, 3]
assert [1], "List should not be empty"
Drag options to blanks, or click blank then click option'
Alen(numbers) == 0
Bnumbers is None
Cnumbers == []
Dlen(numbers) > 0
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if length equals zero instead of greater than zero.
Comparing the list directly to an empty list incorrectly.
3fill in blank
hard

Fix the error in the assert statement that checks if score is between 0 and 100 inclusive.

Python
score = 85
assert 0 <= score and score [1] 100, "Score must be between 0 and 100"
Drag options to blanks, or click blank then click option'
A<=
B==
C>
Dor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'or' which allows invalid scores.
Using '==' which only checks equality, not range.
4fill in blank
hard

Fill both blanks to assert that the string name is not empty and has length less than 10.

Python
name = "Alice"
assert [1] and [2], "Name must be non-empty and shorter than 10 characters"
Drag options to blanks, or click blank then click option'
Alen(name) > 0
Blen(name) == 0
Clen(name) < 10
Dlen(name) > 10
Attempts:
3 left
💡 Hint
Common Mistakes
Using length equal to zero instead of greater than zero.
Checking length greater than 10 instead of less than 10.
5fill in blank
hard

Fill all three blanks to create a dictionary valid_scores with keys as uppercase names and values as scores greater than 50.

Python
scores = {"alice": 60, "bob": 45, "carol": 75}
valid_scores = { [1]: [2] for name, score in scores.items() if score [3] 50 }
Drag options to blanks, or click blank then click option'
Aname.upper()
Bscore
C>
Dname.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using name.lower() instead of upper().
Using score less than or equal to 50.
Swapping keys and values in the comprehension.