Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
odd or even minute
#1
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.
Reply
#2
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?
Reply
#3
Yeah, mod 2. But this doesn’t answer why code that worked yesterday doesn’t today.
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply
#7
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.
Reply
#8
(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]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Having trouble with minute stock data MAZambelli4353 2 2,310 Sep-03-2019, 09:41 AM
Last Post: perfringo
  python crontab remove_all(comment="Minute*") vvarrior 1 2,737 Aug-06-2018, 12:39 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020