Search code examples
pythonassert

assert with error message equivalent in python


Is there any pythonic way to write asserts with messages like in C/CPP:

assert(i <= j && "more participants than medals");

When I try the equivalent I get a pylint error which probably indicates there is a better way (?):

R1726: Boolean condition 'i <= j and "..."' may be simplified to 'i <= j' (simplifiable-condition)

Solution

  • In python you should use this format:

    assert <condition>,<error message>
    

    So in your case it has to be like this:

    assert i <= j,"more participants than medals"