def main(): """Provide the background logic and link the components together.""" while True: score = [None, None, None] introduction() location = navigate() if quiz_check(location, score): score = quiz(location, score) if (score[0] is not None and score[1] is not None and score[2] is not None): if user_binary_choice( "Do you want your score saved in the leaderboard"): c = db_create() leaderboard_entry(c, score) if user_binary_choice("Do you want to see the leaderboard"): if 'c' not in locals(): c = db_create() show_leaderboard(c) if not user_binary_choice("Do you want to play again"): return else: if location != 0: input(""" You've done this room.""") location = navigate(location)
results = c.fetchall() lines = 0 while lines < len(results): print('/' + "¯" * 89 + '\\') print("| {:15} | {:10} | {:13} | {:11} | {:13} | {:10} |".format( 'Username', 'Date', 'Overall Score', 'Maths Score', 'English Score', 'NCEA Score')) for x in range(get_terminal_size().lines - 3): print("| {:15} | {:10} | {:13} | {:11} | {:13} | {:10} |".format( results[lines][0], results[lines][1], results[lines][5], results[lines][2], results[lines][3], results[lines][4])) lines += 1 if lines >= len(results): input("(enter to continue)") clear_screen() return while True: user_input = input("[q]uit, [n]ew page: ").lower() if user_input == 'q': clear_screen() return if user_input == 'n': clear_screen() break clear_line() if __name__ == "__main__": from db_create3 import db_create show_leaderboard(db_create())
def fetch_results(c): """Get the leaderboard with optional filtering by username. Args: c: The cursor object. Returns: The results of the database query """ if user_binary_choice("Do you want to search by username"): username = f"%{get_username()}%" c.execute( """SELECT * FROM `leaderboard` WHERE `username` LIKE ? ORDER BY `scoretotal` DESC, `username` ASC""", [username]) else: c.execute("""SELECT * FROM `leaderboard` ORDER BY `scoretotal` DESC, `username` ASC""") results = c.fetchall() if not results: input(""" No results.""") return results if __name__ == "__main__": from db_create3 import db_create print(fetch_results(db_create()))
"""See leaderboard_entry.__doc__.""" from datetime import date from get_username import get_username def leaderboard_entry(c, score): # noqa: D400, D205 """Insert the user's score, username and the date of completion into the leaderboard. Args: c: The cursor object. score: The score of the user. """ c.execute("INSERT INTO leaderboard VALUES (?, ?, ?, ?, ?, ?)", [ get_username(), date.today(), score[0], score[1], score[2], sum(score) ]) input(""" Your score, username and the date of completion have been entered into the leaderboard""") if __name__ == "__main__": from db_create3 import db_create leaderboard_entry(db_create(), [int(input()), int(input()), int(input())])