jeudi 3 août 2017

if statement how to add a continue

How would I add a continue into the class_ = eqid if eqid in allowed_classes else default_class I don't understand that kind of if statement, I need to add a continue as if eqid == allowed_classes then I want the while loop to stop, I also only want it to print the selected class if eqid == allowed_classes

allowed_classes = set(["N001", "N002", "N003", "N004", "E001", "E002", "E003"])  # etc
default_class = "N004" #If no class is specified it is defaulted to N004

eqid = raw_input('Please swipe your card: ').strip().upper()
class_ = eqid if eqid in allowed_classes else default_class
print("Selected class", class_)

I have tried

eqid = raw_input('Please swipe your card: ').strip().upper()
if eqid in allowed_classes 
    class_ = eqid
    print('Selected class: ', class_)
else default_class

but that doesn't work, won't even run.

FULL CODE:

import csv
import datetime
import os

#Defaults
allowed_classes = set(["N001", "N002", "N003", "N004", "E001", "E002", "E003"])  # etc
default_class = "N004" #If no class is specified it is defaulted to N004

while (1):

#Magnetic Card Reader Output & Attendance (Default = 0)
attendance = '0'
eqid = raw_input('Please swipe your card: ').strip().upper()
class_ = eqid if eqid in allowed_classes else default_class
print("Selected class", class_)


#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 = 0 #If they scan it outside of the dedicated periods it defaults to period 1

#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

#Class Code Reader
dataList = []
with open('J:/attendance/Timetables/'+class_+'.csv', 'r') as csvfile:
  csvreader = csv.reader(csvfile)
  for 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()

(It is formatted correctly just can't get it formatted in here sorry :/)

Aucun commentaire:

Enregistrer un commentaire