|
| 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 |
1 | 35 | print("Welcome to AskPython Quiz")
|
2 | 36 |
|
3 | 37 | # Get user's readiness to play the Quiz
|
|
26 | 60 | print("Wrong Answer :(") # User's answer is incorrect
|
27 | 61 |
|
28 | 62 | # 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?") |
32 | 64 | if answer.lower() == "askpython":
|
33 | 65 | score += 1
|
34 | 66 | print("correct") # User's answer is correct, increment the score
|
35 | 67 | else:
|
36 | 68 | print("Wrong Answer :(") # User's answer is incorrect
|
37 | 69 |
|
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.") |
46 | 99 |
|
47 | 100 | # Farewell message
|
48 | 101 | print("BYE!")
|
0 commit comments