Files
Learn-Python-Programming-Th…/ch11/assertions.py
T
adii1823 3cebc2fe91 ch11
2021-10-28 17:40:37 +05:30

29 lines
657 B
Python

# assertions.py
mylist = [1, 2, 3] # pretend this comes from an external source
assert 4 == len(mylist) # this will break
for position in range(4):
print(mylist[position])
"""
$ python assertions.py
Traceback (most recent call last):
File "assertions.py", line 4, in <module>
assert 4 == len(mylist) # this will break
AssertionError
"""
# asserts with message
assert 4 == len(mylist), f"Mylist has {len(mylist)} elements"
"""
$ python assertions.py
Traceback (most recent call last):
File "assertions.py", line 19, in <module>
assert 4 == len(mylist), f"Mylist has {len(mylist)} elements"
AssertionError: Mylist has 3 elements
"""