(Dec-27-2019, 09:20 PM)Dixon Wrote: [ -> ]This thing is nutty. Again, it is not giving me the proper output. Now it is returning 'Not an odd minute' even though it is. It's an exercise from 'Head First Python'.
When I enter it with Visual Studio, it works. ??? I was using Python's Idle, and that is where the trouble seems to be.
Why don't you use code provided in the exercise? In the exercise there is (not that this is a good idea to use Python as typewriter):
odds = [1, 3, 5, 7, 9,11,13,15,17,19,
21,23,25,27,29,31,33,35,37,39,
41,43,45,47,49,51,53,55,57,59]
One will get different results depending whether 'odds' is assigned without brackets in one line or not.
I understand that this exercise about for-loop and if...else but desired result can be achieved without them as well:
datetime.today().minute % 2 and 'This is an odd minute' or 'This is an even minute'
As already pointed out by ndc85430 there is no point of defining list with odd values - it can be checked with % 2. However, if one absolutely needs this it can be done with much less typing:
>>> nums = range(1, 60, 2)
>>> 31 in nums
True
>>> 30 in nums
False
>>> list(range(1, 60, 2))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59]