Problem with printing statement when condition is false

Terry Reedy tjreedy at udel.edu
Thu Mar 4 16:28:22 EST 2021


On 3/4/2021 3:15 PM, Terry Reedy wrote:
> On 3/4/2021 12:10 PM, Quentin Bock wrote:
> 
>> I won't paste the code into
>> this because it's 164 lines so I will attach a file copy,
> 
> The alternative to posting too much is to reduce your code to the 
> minimum needed to exhibit the behavior you want to change.  Doing so may 
> reveal to you the solution.  It will certainly make it easier for anyone 
> else to help you and will make any answer more useful to other readers.

Quentin privately sent me 12 lines (which should have been posted here 
instead), which can be reduced to the following 4 that exhibit his bug.

if a == b:
     print('correct')
     if a != b:
         print('incorrect')

The bug is a != b will never be true when a == b and will not be tested 
when a != b.  The fix is to use else.

if a == b:
     print('correct')
else:
     print('incorrect')

This should not be reduced to a conditional expression since different 
code follows the print statements in the actual example.

--
Terry Jan Reedy




More information about the Python-list mailing list