python IndexError list index out of range + how to break a while then also restart the while statement -
please swipe card: chend151 traceback (most recent call last): file "c:\attendance\attendance 3.py", line 42, in <module> clcode = datalist[period][d] indexerror: list index out of range
i error when running code, can't seem find problem :/
my code:
import csv import datetime import os class_ = 'n004' while (1): #day & time checker format = "%h%m%s" format_ = "%h%m" today = datetime.datetime.today() s = today.strftime(format) #time in 24hour s2 = today.strftime(format_) d = datetime.datetime.today().weekday() #day of week (0-5) period = -1 #if scan outside of dedicated periods defaults unknown period` #period checker if "084500" < s < "094000": period = 0 if "094000" < s < "104000": period = 1 if "112000" < s < "121500": period = 2 if "121500" < s < "131500": period = 3 if "133500" < s < "143000": period = 4 #magnetic card reader output & attendance (default = 0) attendance = '0' eqid = str(input('please swipe card: ')) #class code reader datalist = [] open(class_+'.csv', 'r') csvfile: csvreader = csv.reader(csvfile) row in csvreader: datalist.append(row) csvfile.close() #class code clcode = datalist[period][d] #csv writer ofile = open('attendance.csv', "a") writer = csv.writer(ofile, delimiter=',') writer.writerow([eqid, period+1, clcode, attendance]) ofile.close() `#os.rename("j:/attendance/attendance.csv", "j:/g_drive/attendance.csv")` sorry formatting still can't seem understand how formatting works code :c also, question, in while statement want have if statement `if eqid == "n004": class_ = "n004" break
i know indentation off how break loop restart it? want if enter n004
breaks loop sets class_ = 'n004'
restarts while loop , if don't enter n004
loop continue normal.
thank you, makes sense.
edit: try explain want, think have more 1 question, firstly thank helping. want have 1 print statement asks user swipe card or insert username , in same print statement asks them type current class in (eg n004') , whatever type gets exported csv file, have setup already, when type in class example n004 want while loop break n004 doesnt exported csv file. i'm not best @ explaining want in 300 characters
eqid = str(input('please swipe card: ')) if eqid == "n004": class_ = "n004" continue else: print ("didn't work")
Comments
Post a Comment