Q:

NameError: global name 'program_startdate' is not defined

I am working on my python script to get the list of time formats that I stored in sqlite3 database.
I have got a problem with the variable program_startdate which is under the loop for each row in programs.
When I try this:
#get the programs list
cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel=?', [channel])
programList = list()
programs = cur

for row in programs:
   program = row[1].encode('ascii'), str(row[2]), str(row[3])
   program_startdate = (str(row[2]))
   program_endDate = (str(row[3]))
   programList.append(program)
   print(str(row[2]))
cur.close()

idx = str(programList)

# find nearest half hour
viewStartDate = datetime.datetime.now()
viewStartDate -= datetime.timedelta(minutes = viewStartDate.minute % 30, seconds = viewStartDate.second)

#convert the datetime object between start and end date
startDelta = program_startDate - viewStartDate
stopDelta = program_endDate - viewStartDate

I tried to get the variable program_startdate outside of the loop, but I get an error:  NameError: global name 'program_startDate' is not defined.
The error are jumping on this line:
startDelta = program_startDate - viewStartDate

Can you please advice me how I can get the variable outside of the loop?
EDIT: it show like this:
19:00:06 T:5028  NOTICE: 20140520170000
19:00:06 T:5028  NOTICE: 20140520173000
19:00:06 T:5028  NOTICE: 20140520200000
19:00:06 T:5028  NOTICE: 20140520230000
19:00:06 T:5028  NOTICE: 20140521000000
19:00:06 T:5028  NOTICE: 20140521003000
19:00:06 T:5028  NOTICE: 20140521010000
19:00:06 T:5028  NOTICE: 20140521013000
19:00:06 T:5028  NOTICE: 20140521020000
19:00:06 T:5028  NOTICE: 20140521023000
19:00:06 T:5028  NOTICE: 20140521030000
19:00:06 T:5028  NOTICE: 20140521040000
19:00:06 T:5028  NOTICE: 20140521043000
19:00:06 T:5028  NOTICE: 20140521050000
19:00:06 T:5028  NOTICE: 20140521053000
19:00:06 T:5028  NOTICE: 20140521060000
19:00:06 T:5028  NOTICE: 20140521063000
19:00:06 T:5028  NOTICE: 20140521070000
19:00:06 T:5028  NOTICE: 20140521073000
19:00:06 T:5028  NOTICE: 20140521080000
19:00:06 T:5028  NOTICE: 20140521083000
19:00:06 T:5028  NOTICE: 20140521090000
19:00:06 T:5028  NOTICE: 20140521110000
19:00:06 T:5028  NOTICE: 20140521120000
19:00:06 T:5028  NOTICE: 20140521123000
19:00:06 T:5028  NOTICE: 20140521130000
19:00:06 T:5028  NOTICE: 20140521133000

A:

Move your time-related computations into your for loop to compute for each program in your list.
#get the programs list
cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel=?', [channel])
programList = list()
programs = cur.fetchall()

for row in programs:
    program = row[1].encode('ascii'), str(row[2]), str(row[3])

    program_startdate = datetime.datetime.strptime(str(row[2]), "%Y%m%d%M%S")
    program_endDate = datetime.datetime.strptime(str(row[3]), "%Y%m%d%M%S")
    programList.append(program)
    print(str(row[2]))

    # find nearest half hour
    viewStartDate = datetime.datetime.now()
    viewStartDate -= datetime.timedelta(minutes = viewStartDate.minute % 30, seconds = viewStartDate.second)

    #convert the datetime object between start and end date
    startDelta = program_startDate - viewStartDate
    stopDelta = program_endDate - viewStartDate
    print startDelta, stopDelta   # check if you're getting the result you want

cur.close()

idx = str(programList)

