Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 6039d19

Browse files
Merge pull request #751 from amersbahi/quiz-game
created a database to store and modified existed function
2 parents 642c400 + d539263 commit 6039d19

File tree

2 files changed

+91
-11
lines changed

2 files changed

+91
-11
lines changed

projects/Quiz Game/main.py

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
# import the sqlite3 to reterive previous scores
2+
import sqlite3
3+
# create Conn to have the database file
4+
def create_connection(db_file):
5+
""" create a database connection to the SQLite database """
6+
conn = None
7+
try:
8+
conn = sqlite3.connect(db_file)
9+
except sqlite3.Error as e:
10+
print(e)
11+
return conn
12+
# store the player's score with their name
13+
def save_score(conn, name, score):
14+
"""
15+
Save the player's score to the database
16+
"""
17+
sql = ''' INSERT INTO scores(name, score)
18+
VALUES(?,?) '''
19+
cur = conn.cursor()
20+
cur.execute(sql, (name, score))
21+
conn.commit()
22+
return cur.lastrowid
23+
# recall the previous scores to display them
24+
def get_all_scores(conn):
25+
"""
26+
Query all rows in the scores table
27+
"""
28+
cur = conn.cursor()
29+
cur.execute("SELECT * FROM scores ORDER BY score DESC")
30+
31+
rows = cur.fetchall()
32+
return rows
33+
34+
# The beginning of the game
135
print("Welcome to AskPython Quiz")
236

337
# Get user's readiness to play the Quiz
@@ -26,23 +60,42 @@
2660
print("Wrong Answer :(") # User's answer is incorrect
2761

2862
# Question 3
29-
answer = input(
30-
"Question 3: What is the name of your favourite website for learning Python?"
31-
)
63+
answer = input("Question 3: What is the name of your favourite website for learning Python?")
3264
if answer.lower() == "askpython":
3365
score += 1
3466
print("correct") # User's answer is correct, increment the score
3567
else:
3668
print("Wrong Answer :(") # User's answer is incorrect
3769

38-
# Display the result and user's score
39-
print(
40-
"Thank you for Playing this small quiz game, you attempted",
41-
score,
42-
"questions correctly!",
43-
)
44-
mark = int((score / total_questions) * 100)
45-
print(f"Marks obtained: {mark}%")
70+
# Display the result and user's score
71+
print("Thank you for Playing this small quiz game, you attempted", score, "questions correctly!")
72+
mark = int((score / total_questions) * 100)
73+
print(f"Marks obtained: {mark}%")
74+
75+
# Getting the player's name and score to insert into the database
76+
player_name = input("Enter your name: ")
77+
player_score = score
78+
79+
database = "quiz_game.db"
80+
81+
# Create a database connection
82+
conn = create_connection(database)
83+
84+
if conn is not None:
85+
# Save the player's score
86+
save_score(conn, player_name, player_score)
87+
88+
# Display all scores
89+
print("Previous scores:")
90+
scores = get_all_scores(conn)
91+
for row in scores:
92+
print(f"Name: {row[1]}, Score: {row[2]}, Date: {row[3]}")
93+
94+
conn.close()
95+
else:
96+
print("Error! Cannot create the database connection.")
97+
else:
98+
print(" Please, when you're ready, enter the game again.")
4699

47100
# Farewell message
48101
print("BYE!")

projects/Quiz Game/setup_db.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sqlite3
2+
3+
def create_database():
4+
# Connect to the SQLite database (it will create the file if it doesn't exist)
5+
conn = sqlite3.connect('quiz_game.db')
6+
7+
# Create a cursor object
8+
cursor = conn.cursor()
9+
10+
# Create a table to store players' scores
11+
cursor.execute('''
12+
CREATE TABLE IF NOT EXISTS scores (
13+
id INTEGER PRIMARY KEY AUTOINCREMENT,
14+
name TEXT NOT NULL,
15+
score INTEGER NOT NULL,
16+
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
17+
)
18+
''')
19+
20+
# Commit the changes and close the connection
21+
conn.commit()
22+
conn.close()
23+
24+
print("Database and table created successfully.")
25+
26+
if __name__ == "__main__":
27+
create_database()

0 commit comments

Comments
 (0)