Apr-04-2022, 09:56 PM
(A leap year is divisible by 4) and (a leap year is not divisible by 100 or a leap year divisible by 400).
Natural language is unsuitable for programming tasks because ambiguities and inaccuracies are possible.
I think it's still possible to understand this wrong.
The other problem were many newcomers are struggling with, are expressions in if-conditions.
A precise description of this is here: https://docs.python.org/3/library/stdtyp...ue-testing
Natural language is unsuitable for programming tasks because ambiguities and inaccuracies are possible.
I think it's still possible to understand this wrong.
The other problem were many newcomers are struggling with, are expressions in if-conditions.
if
wants to have a Boolean. If it's not a boolean, then it's converted to a boolean.A precise description of this is here: https://docs.python.org/3/library/stdtyp...ue-testing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
some_value = 42 by4 = 4 by3 = 3 # bool(0) == False # bool(-1) == True # bool(1) == True # if by4, will call implicit if bool(by4), because by4 is not a boolean. # if requires a True or False # if an object is of the type Boolean, # then bool is called with the object, to get a boolean back # False or (3) if some_value % by4 = = 0 or by3: print ( "Always True because by3 is 3 and bool(3) is True" ) print ( "The first expression is False" ) print ( "And the second expression is the bug" ) # You want: if some_value % by4 = = 0 and some_value % by3 = = 0 : print ( "You can divide 42 by 3 and 42 by 4" ) # no, you can't! |
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!