Python Forum

Full Version: odd or even minute
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
from datetime import datetime
import time
import random
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

right_this_minute = datetime.today().minute

for i in range(5):
    if right_this_minute in odds:
        print("This is an odd minute")
    else:
        print("Not an odd minute")

    wait_time = random.randint(1,60)
    time.sleep(wait_time)
This is a common schooling script I believe. But it has stopped working properly for reasons unknown. When the computer clock changes the minute, this code has stopped coming up with the correct print statement.
I assume you wanted some kind of sequence (set, list or tuple really) on line 4, in which case you're not declaring it properly (e.g. if you wanted a set, you're missing the enclosing curly braces). Why even have the variable odds in the first place though? Why not just check if the value is odd/even by checking its divisibility by 2?
Yeah, mod 2. But this doesn’t answer why code that worked yesterday doesn’t today.
With line 7 outside of the loop, you'll always be checking the same number for evenness. When it worked yesterday, that could have resulted from the random wait time. Move line 7 into your for loop and it should work.
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.
What have you done to debug the problem?

Also, it would be a good idea to use a version control system (such as Git) to keep track of the changes to your code.
Lines 4 and 5 are two different lines, hence the sequence of odd numbers is not complete. Add a backslash at the end of line 4 or use parentheses odds = ( .... Print the variable odds to see what I mean.
(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]
When you encounter such a thing you must add debug statements in you program to see if really is happening what should happen. For example: test whether the "odds" tuple really has the right content.
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
# DEBUG: test the contents of odds
print(odds)
Output:
(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31)
There you see what Gribouillis said. Only line 4 is part of the assignment. It must be so that yesterday you tested between 0 and 31 minutes after the whole hour. And the next day after 31 minutes.
Now look at the other hints people gave you because there are other problems in your program. Insert debug statements before each output that is not right.
When I entered 'odds' on one line it worked. I guess the book is in error where is says on page 13 that it's OK to enter the 'odds' numbers on several lines.

Thanks to all for the valuable input. I'll mark solved.
Pages: 1 2