Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assert with error message equivalent in python [duplicate]

Tags:

python

assert

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)
like image 378
OrenIshShalom Avatar asked Jun 15 '26 18:06

OrenIshShalom


1 Answers

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"
like image 85
Wouter Avatar answered Jun 17 '26 10:06

Wouter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!