Mwnci assert() Function

Syntax

assert(expr,[msg])

Parameters

 

Parameter
Definition
expr true/false result of a test
msg Optional message to display for an assertion failure when the result is false.

Description

The assert() function exits the program immediately with a non-zero status if expr is false optionally displaying msg to standard error.

assert() is used when debugging code.

assert() lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.

Example

x=”goodbye”
assert(x == “hello”, “x should equal ‘goodbye’\n”)

Assertion Error: x should equal ‘goodbye’