Question:
Raise Assertion error when trying to verify that every number in the list [2,4,6,8,1] is even.
Use “assert expression” to make assertion
Program:
1 2 3 4 5 |
li = [2,4,6,8,1] for i in li: assert i%2==0 |
Explanation:
The Assert Statement: When python encounters an assert statement, Python evaluates the accompanying expression, which is hopefully true. If the expression is false, Python raises an AssertionError exception.
Output:
1 2 3 4 5 6 7 |
Traceback (most recent call last): File "assert.py", line 4, in <module> assert i%2==0 AssertionError |
Leave a Reply