def list(self, quizList, user): cprint("Quiz List") pline("~") options = [] for q in quizList: cprint(str(q[0]) + ". " + q[1]) options.append(int(q[0])) pline("=") cprint("Choose a quiz to take") pline("=") quizChoice = input("> ") pline("=") if isinstance(quizChoice, int) != True: try: quizChoice = int(quizChoice) except Exception: cprint("Option should be a number", "lightred") pline("=") self.list(self.quizList) if quizChoice in options: quiz.Quiz(quizChoice, user) else: cprint("Not a quiz", "lightred") pline("=") self.list(self.quizList)
def display(self, data, width, user): size = int(width / 5) print( "\n{qName:^{sizeTwo}} {score:^{size}} {diff:^{size}} {date:^{size}}" .format(size=str(size), sizeTwo=str(size * 2), qName="Quiz Name", diff="Difficulty", score="Score", date="Date")) cprint("~" * (width - 4), "lightcyan") for result in data: print( "{qName:^{sizeTwo}} {score:^{size}} {diff:^{size}} {date:^{size}}" .format(size=str(size), sizeTwo=str(size * 2), qName=str(result[3]), diff=str(result[6]), score=str(result[5]) + "%", date=str(result[7]))) Home(user)
def __init__(self): pline("=") cprint("Welcome to the GeneralQuiz") cprint("Created by William Neild") pline("=") cprint("Please choose an option from the following") pline("~") self.options()
def options(self): cprint("1. Login or Register an Account") cprint("2. Quit Application") pline("=") option = input("> ") if option == "1": Account() elif option == "2": cprint("Cya Later!") database.close() exit() else: pline("=") cprint(" Not a valid option, Try again ", "lightred") pline("=") self.options()
def options(self): cprint("1. Login") cprint("2. Register") cprint("3. Back to Main Menu") pline("=") option = input("> ") pline("=") if option == "1": self.login() elif option == "2": # security.Account.register() self.register() elif option == "3": Main() else: pline("=") cprint(" Not a valid option, Try again ", "lightred") pline("=") self.options()
def __init__(self, user): self.user = user pline("=") width = int(get_terminal_size()[0]) + 1 if width >= 25: if width > 65: size = "big" else: size = "small" for line in open('generalQuiz/data/' + str(size) + '.txt', 'r'): cprint(line.rstrip('\n'), "lightgreen") cprint("General Quiz", "lightgreen") pline("=") cprint("Welcome Back " + self.user["fname"]) pline("=") self.options(user)
def register(self): cprint("Register an account") pline("=") cprint("Enter your First Name") fname = input("> ") cprint("Enter your Last Name") lname = input("> ") cprint("Enter your Age") age = input("> ") cprint("Enter a password") password = input("> ") cprint("Confirm your password") confPassword = input("> ") cprint("Enter your year group") year = input("> ") account = security.Account.validateReg(fname, lname, password, confPassword, age, year) if account != []: pline("!", "lightred") for i in account: cprint(" " + i + " ", "lightred") pline("!", "lightred") pline(" ") self.register() else: username = str(fname[:3]) + str(age) security.Account.register(username, password, fname, lname, age, year) cprint("Account created. Your username is: " + username, "lightgreen") self.login()
def login(self): cprint("Login to your account") pline("=") cprint("Enter Username") cprint("~~~~~~~~~~~~~~") username = input("> ") cprint("Enter Password") cprint("~~~~~~~~~~~~~~") password = input("> ") userLogin = security.Account.login(username, password) if userLogin != False: Home(userLogin) else: cprint(" Login details incorrect ", "lightred") self.login()
def __init__(self): pline("=") cprint("Do you want to Login or Register?") pline("~") self.options()
def options(self, user): cprint("Choose an option from the following") pline("~") cprint("1. Take a Quiz") cprint("2. Quiz results") cprint("3. Profile Options") cprint("4. Exit Application") pline("=") option = input("> ") pline("=") if option == "1": Quiz(user) self.options(user) elif option == "2": Results(user) elif option == "3": cprint("Profile Options Page") elif option == "4": database.close() exit() else: cprint("Option Doesn't Exist", "lightred") Home(user)
def startQuiz(self, quiz, questions, difficulty, user): pline("=") shuffle(questions) currentQuestion = 0 numOfOptions = difficulty + 1 score = 0 for question in questions: answerAccepted = False currentQuestion += 1 cprint(str(currentQuestion) + ". " + str(question[2])) pline("~") correctAns = question[3] otherAns = loads(question[4]) shuffle(otherAns) options = [] for i in range(0, difficulty): options.append(otherAns[i]) options.append(correctAns) shuffle(options) opt = 0 for option in range(0, numOfOptions): opt += 1 cprint(str(opt) + ". " + options[option]) userAnswer = None while answerAccepted != True: try: userAnswer = int(input("Answer > ")) if (userAnswer <= numOfOptions) != True: raise if (userAnswer >= 1) != True: raise answerAccepted = True except Exception: cprint("That was not an Answer, please try again", "lightred") if options[userAnswer - 1] == correctAns: score += 1 pline("=") cprint("Quiz Finished") totalQuestions = len(questions) totalCorrect = score totalPercentage = (totalCorrect / totalQuestions) * 100 if difficulty == 1: diff = "EASY" elif difficulty == 2: diff = "MEDIUM" elif difficulty == 3: diff = "HARD" else: print("Error, Difficulty somehow got messed up!") cprint("Total Questions: " + str(totalQuestions)) cprint("Correct Answers: " + str(totalCorrect)) cprint("Percent Correct: " + str("{0:.2f}".format(round(totalPercentage, 2))) + "%") database.add_result(quiz[0], quiz[1], user["id"], totalCorrect, totalPercentage, diff) pline("=")
def startPage(self, quiz, questions, user): cprint("Choose your level") pline("~") cprint("1. Easy (2 Options)") cprint("2. Medium (3 Options)") cprint("3. Hard (4 Options)") pline("=") difficulty = None while isinstance(difficulty, int) != True: try: difficulty = int(input("> ")) except Exception: cprint("That was not a difficulty, please try again", "lightred") pline("=") print("\n") cprint("Starting " + quiz[1] + " Quiz") print("\n") self.startQuiz(quiz, questions, difficulty, user)