0

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)
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • 2
    [`assert` statement documentation](https://docs.python.org/3.10/reference/simple_stmts.html#the-assert-statement) – deceze Jan 11 '22 at 07:11
  • 2
    `assert i <= j , "more participants than medals"` – Deven Ramani Jan 11 '22 at 07:13
  • 2
    It just feels like you’ve randomly copied C++ code into Python without having even consulted the documentation. It’s arguably somewhat abstract, but if read carefully, that short paragraph in the docs explains exactly how to write a proper `assert` statement. – deceze Jan 11 '22 at 07:23
  • 1
    "emphasize that more than one expression can be tested, but the message itself is marginalized there" No, they don't; they show that exactly one or two expressions are permissible in the grammar; and then they show (by equivalent code) that the second expression, when provided, **is not** tested (but instead is the message to use). – Karl Knechtel Jan 11 '22 at 08:15

1 Answers1

5

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"
Wouter
  • 769
  • 3
  • 9