exceptions added for user input

This commit is contained in:
2019-08-02 18:54:27 +02:00
parent 0cb5e2f73f
commit caf650eaef
4 changed files with 91 additions and 33 deletions
+22
View File
@@ -0,0 +1,22 @@
class NotANumberException(Exception):
pass
class SubjectNotExistingException(Exception):
pass
class CategoryNotExistingException(Exception):
pass
class WrongCaseException(Exception):
pass
def convertible(string):
try:
float(string)
return True
except ValueError:
raise NotANumberException
+11 -2
View File
@@ -2,6 +2,7 @@ import subject
import user_IO import user_IO
import serializer import serializer
import encryption import encryption
import exceptions
import os import os
subjects = [] subjects = []
@@ -13,9 +14,8 @@ try:
string = encryption.decrypt(password, file.read()) string = encryption.decrypt(password, file.read())
serializer.deserialize(string) serializer.deserialize(string)
while True: while True:
try:
user_input = user_IO.user_input() user_input = user_IO.user_input()
input_or_output = user_input[0] input_or_output = user_input[0]
if input_or_output == "input": if input_or_output == "input":
@@ -38,12 +38,21 @@ try:
_main = user_input[3] _main = user_input[3]
_oral = user_input[4] _oral = user_input[4]
subjects.append(subject.Subject(_subject, _main, _oral)) subjects.append(subject.Subject(_subject, _main, _oral))
else:
raise exceptions.WrongCaseException
elif input_or_output == "output": elif input_or_output == "output":
_subject = user_input[1] _subject = user_input[1]
subject_get = subject.get_subject(subjects, _subject) subject_get = subject.get_subject(subjects, _subject)
user_IO.user_output(subject_get.return_average_subject) user_IO.user_output(subject_get.return_average_subject)
elif input_or_output == "exit": elif input_or_output == "exit":
break break
else:
raise exceptions.WrongCaseException
except (exceptions.NotANumberException, exceptions.CategoryNotExistingException,
exceptions.SubjectNotExistingException, exceptions.WrongCaseException) as e:
user_IO.exception_raised(e)
with open(filepath, 'w') as file: with open(filepath, 'w') as file:
_list = [serializer.serialize(_subject) for _subject in subjects] _list = [serializer.serialize(_subject) for _subject in subjects]
string = encryption.encrypt(password, str(_list)) string = encryption.encrypt(password, str(_list))
+7 -1
View File
@@ -1,4 +1,5 @@
from datetime import datetime as str_date from datetime import datetime as str_date
from exceptions import SubjectNotExistingException, CategoryNotExistingException
class Subject: class Subject:
@@ -31,6 +32,11 @@ class Subject:
grade_sum += self.return_average_of_category(i) grade_sum += self.return_average_of_category(i)
return grade_sum / rating_sum return grade_sum / rating_sum
def category_existing(self, category):
if category in self.grades:
return True
raise CategoryNotExistingException
class Grade: class Grade:
def __init__(self, grade, name, date=str(str_date.now())): def __init__(self, grade, name, date=str(str_date.now())):
@@ -43,4 +49,4 @@ def get_subject(liste, name):
for subject in liste: for subject in liste:
if subject.name == name: if subject.name == name:
return subject return subject
return None raise SubjectNotExistingException
+21
View File
@@ -1,3 +1,6 @@
import exceptions
def user_input(): def user_input():
todo = input("Ok, do you want to see them or to edit them [see/edit/exit]?\n") todo = input("Ok, do you want to see them or to edit them [see/edit/exit]?\n")
if todo == "see": if todo == "see":
@@ -14,22 +17,28 @@ def user_input():
main_subject = True main_subject = True
elif main_subject == 'n': elif main_subject == 'n':
main_subject = False main_subject = False
else:
raise exceptions.WrongCaseException
oral_exam = input("Will there be an oral exam in this subject [y/n]? \n") oral_exam = input("Will there be an oral exam in this subject [y/n]? \n")
if oral_exam == 'y': if oral_exam == 'y':
oral_exam = True oral_exam = True
elif oral_exam == 'n': elif oral_exam == 'n':
oral_exam = False oral_exam = False
else:
raise exceptions.WrongCaseException
return "input", "subject", subject, main_subject, oral_exam return "input", "subject", subject, main_subject, oral_exam
elif user_add == "category": elif user_add == "category":
subject = input("To which subject do you want to add the category? \n") subject = input("To which subject do you want to add the category? \n")
category = input("Which category do you want to add? \n") category = input("Which category do you want to add? \n")
rating = float(input("Rating? \n")) rating = float(input("Rating? \n"))
if exceptions.convertible(rating):
return "input", "category", subject, category, rating return "input", "category", subject, category, rating
elif user_add == "grade": elif user_add == "grade":
subject = input("To which subject do you want to add the grade? \n") subject = input("To which subject do you want to add the grade? \n")
category = input("To which category do you want to add it? \n") category = input("To which category do you want to add it? \n")
grade = input("Grade? \n") grade = input("Grade? \n")
grade_name = input("What's the name of the grade? \n") grade_name = input("What's the name of the grade? \n")
if exceptions.convertible(grade):
return "input", "grade", subject, category, grade, grade_name return "input", "grade", subject, category, grade, grade_name
elif todo == "exit": elif todo == "exit":
return "exit" return "exit"
@@ -49,3 +58,15 @@ def user_output(subject, categories, average, average_subject):
def password(): def password():
print("What is your password?") print("What is your password?")
return input() return input()
def exception_raised(exception):
print("Dear user you've raised an exception. The cause of the exception is:")
if exception is exceptions.SubjectNotExistingException:
print("The subject you tried to access is not created")
elif exceptions is exceptions.CategoryNotExistingException:
print("The category you tried to add something is non-existent in this subject")
elif exception is exceptions.NotANumberException:
print("The number you gave the program is not a number")
elif exception is exceptions.WrongCaseException:
print("This was not one of the available choices")