-3

I am trying to doing a project using nested if. But it's not working. why? My code is,

schedule = Schedule.objects.all()
    for c in schedule :
        p = c.poll
        e = c.end_time
        s = c.start_time
        n = c.no_of_response
        now = timezone.now()
        #phn = Response.objects.filter(poll = p).exclude(sid = 'Null').count()
        if (c.start_time <= now) & (now <= c.end_time):
            if n == 0:
                c.poll.status='Running'
                c.poll.save()
ilse2005
  • 11,189
  • 5
  • 51
  • 75
naveen
  • 71
  • 1
  • 5
  • 2
    What is not working? Please provide more information. – ilse2005 Apr 15 '16 at 08:56
  • You haven't included any information about what `no_of_response` is, (i'll leave my answer there because I still think that is a mistake) – Sayse Apr 15 '16 at 09:11
  • if (c.start_time <= now) & (now <= c.end_time): c.poll.status='Running' c.poll.save() This code is working. But when I put nested if, that is if n==0: , inside the first if condition, then the whole code is not working. Why this happened? – naveen Apr 15 '16 at 09:17
  • It is working, but `n` is never 0, and as I mention above, you haven't included any information about what `no_of_response` *is* – Sayse Apr 15 '16 at 09:18

1 Answers1

2

You're doing a bit comparison with &, you probably want to use and (or &&)

(c.start_time <= now) and (now <= c.end_time)

or better yet

c.start_time <= now <= c.end_time
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • This code is correct. But doesn't check the if n==0: . Why? Sorry for my bad english – naveen Apr 15 '16 at 09:03
  • @naveen - Probably because that never evaluates for true for whatever reason, you might want to try using `datetime.now()` instead – Sayse Apr 15 '16 at 09:04
  • This code is working. if (c.start_time <= now) & (now <= c.end_time): c.poll.status='Running' c.poll.save() – naveen Apr 15 '16 at 09:06