A

Video

3ai

The purpose of this program is to create a system where the highest score of each user is stored and displayed using a table.

3aii

The video demonstrates a user (Jay) gaining a score. If the score is their highest score, it will be added to the table. Since they gained a higher score than they previously did, their score was added to the table.

3aiii

The input demonstrated in the video is the user's score that they will gain at the end of the game. The output is the highest score stored in the table.

B

3bi

highscore1 = Highscore(username='sreeja', hscore=7)
highscore2 = Highscore(username='ekam', hscore=7)
highscore3 = Highscore(username='tirth', hscore=7)
highscore4 = Highscore(username='mani', hscore=7)
highscores = [highscore1, highscore2, highscore3, highscore4]

3bii

"""Builds sample user/note(s) data"""
for highscore in highscores:
    try:
        highscore.create()
    except IntegrityError:
        '''fails with bad or duplicate data'''
        db.session.remove()
        print(f"Records exist, duplicate email, or error: {highscore.username}")

3biii

The program includes a list named "highscores," which contains 4 users and their high scores. The provided code snippet employs a for loop to traverse through the list and execute the create method from the "Highscore" class. This creates a new entry for each "highscore" in the SQL table labelled "highscores". This functionality is crucial since it allows the program to generate a database with data that can be sorted using SQLalchemy functions. As a result, a table of high scores can be generated, which can be accessed through a get request.

3biv

The data in the list represents users and their single highest core.

3bv

C

3ci

def create(self):
    try:
        db.session.add(self)
        db.session.commit()
        return self
    except IntegrityError:
        db.session.remove()
        return None

3cii

def initHighscores():
    with app.app_context():
        """Create database and tables"""
        db.init_app(app)
        db.create_all()

3ciii

The "create" function is being called as a method of an object, where it adds itself to the session, commits the changes to the database, and returns itself if successful. If there is an integrity error, it removes the session and returns None. In the "initHighscores" function, the database is initialized and tables are created using the Flask app context and the SQLAlchemy library's "create_all" method.

3civ

This algorithm creates an instance of a class and adds it to a database session. If the session commit fails due to an IntegrityError, the session is removed and the function returns None. Additionally, it initializes a Flask app context and creates a database and its tables using SQLAlchemy.

D

3di

img1


img2

3dii

The first call checks if the user's score will be added to the table since they got the highest score possible.

The second call checks that the user's score does not get added since it is the user's lowest score.

3diii

The user's score gets added to the "highscores" table in the first call, while the user's score does not get added to the "highscores" table in the second call