Handle ValueErrors and rewrite code

This commit is contained in:
Abdulkadir Furkan Şanlı 2018-12-30 09:28:16 +01:00
parent be5ff7177c
commit b7611be5ee
No known key found for this signature in database
GPG Key ID: B0B350A3CD74C184
2 changed files with 35 additions and 47 deletions

View File

@ -7,5 +7,5 @@ To-Do List
- [ ] Flexible mandatory subjects
## v0.3
- [ ] make script functional
- [x] make script functional (sorta)
- [ ] timetable editor (add & remove lessons, view timetable)

View File

@ -28,30 +28,27 @@ def connect_database(file):
return conn
def get_lower_weekday():
def request_id(conn):
"""
Returns current weekday's name as lowercase string.
"""
day = time.strftime("%A")
lower_day = day.lower()
return lower_day
def check_id(conn, id):
"""
Returns True if existing ID, False if not.
Request student ID, validate, and return.
:param conn: Connection object
:param id: ID of student, int
"""
try:
id = int(input("Please enter student ID: "))
cur = conn.cursor()
cur.execute("SELECT id FROM students")
ids = cur.fetchall()
for x in ids:
# ids are single value tuples
if id == x[0]:
return True
return id
else:
return False
raise ValueError
except ValueError:
print("Incorrect ID, try again.")
request_id(conn)
def select_final_time(conn, day, id):
@ -86,7 +83,7 @@ def select_final_time(conn, day, id):
def parse_time_string(timestring):
"""
Parses given 24h time string of format "HH:MM" into time_struct.
Parses given 24h time string of format "HH:MM" into struct_time.
:param timestring: time string "HH:MM"
"""
timestruct = time.strptime(timestring, "%H:%M")
@ -96,35 +93,26 @@ def parse_time_string(timestring):
def main():
print("ib-clearance")
print("============")
db = input(
"Please enter name of database (in same folder as program): ")
conn = connect_database(db)
y = "y"
while y == "y":
try:
id = int(input("Please input the student ID number: "))
conn = connect_database("database.db")
if check_id(conn, id):
day = get_lower_weekday()
raw_time = select_final_time(conn, day, id)
# if raw_time is None, student has no lessons today
if raw_time != None:
final_time = parse_time_string(raw_time)
current_time = parse_time_string(time.strftime("%H:%M"))
if current_time > final_time:
print("Clear to leave.")
while True:
id = request_id(conn)
day = time.strftime("%A").lower()
finish_time = select_final_time(conn, day, id)
current_time = time.strftime("%H:%M")
if finish_time == None:
print("Student has no lessons today, clear to leave.")
elif parse_time_string(finish_time) < parse_time_string(current_time):
print("Student has finished for today, clear to leave.")
else:
print("Student does not have clearance.")
elif raw_time == None:
print("Clear to leave.")
else:
print("Student does not exist in the database. Please try again.")
except ValueError:
print("That was not an integer, please try again.")
print("Student still has lessons, clearance not granted.")
y = input("Press y key followed by enter to run again.")
if input("Enter y to run again.") != "y":
break
# Run main() if script is run standalone.
if __name__ == "__main__":
main()