-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz_with_sql.py
126 lines (104 loc) · 3.42 KB
/
quiz_with_sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import random
import mysql.connector
# Connect to the MySQL database
conn = mysql.connector.connect(
host='localhost', # Change this if your MySQL server is hosted elsewhere
user='your_username', # Your MySQL username
password='your_password', # Your MySQL password
database='quiz_game' # Your database name
)
cursor = conn.cursor()
print('*' * 26)
print('*' * 10, "QUIZ", '*' * 10)
print('*' * 26)
user_count = 0
all_score = {}
all_gifts = {}
add_cash = {}
while True:
user_count += 1
Name = input("Enter Your Name: ").upper()
# Define questions (same as before)
questions = {
# (Your questions go here...)
}
score = 0
qno = 1
score_report = {}
report = {}
for q in questions:
print(q)
valid_options = ['a', 'b', 'c', 'd']
while True:
ch = input("Choose an option: ").lower()
if ch in valid_options:
if ch == questions[q]:
print("Correct")
score += 1
score_report[qno] = 1
report[qno] = "Correct"
else:
print("Incorrect")
score -= 0.5
score_report[qno] = 0.5
report[qno] = "Incorrect"
break
else:
print("Enter a valid option (a, b, c, or d)")
qno += 1
print()
print()
wrong_count = 0
correct_count = 0
for i in report.keys():
print(i, ' ' * 20, ":" * 5, report[i], ":" * 5, ' ' * 20, score_report[i])
if report[i] == "Incorrect":
wrong_count += 1
elif report[i] == "Correct":
correct_count += 1
print()
print(f"TOTAL CORRECT: {correct_count} TOTAL INCORRECT: {wrong_count}")
print("Total Points:", score)
all_score[Name] = score
print()
# Handle rewards and gifts
gift = None
additional_cash = None
if score == 15:
print("You Rocked it!")
print(f"{Name} You won CASH Prize:", score * 100)
gift_items = ["Smart Watch", "OTT subscription", "Headset", "Additional Cash Back"]
gift = random.choice(gift_items)
if gift == gift_items[3]:
additional_cash = 1000
print("You Have Received Additional Cash Back of ₹:", additional_cash)
# ... (Handle other gifts)
# (Handle other score ranges...)
# Store the results in the database
cursor.execute('''
INSERT INTO quiz_results (name, score, gift, additional_cash)
VALUES (%s, %s, %s, %s)
''', (Name, score, gift, additional_cash))
conn.commit() # Commit changes to the database
print()
ch = input("Do you want to play the quiz again? Press 'y' or 'n': ")
if ch == 'n':
break
# Fetch and display all user data
cursor.execute("SELECT * FROM quiz_results")
rows = cursor.fetchall()
print("REPORT")
print("=" * 50)
print(f"Total Users Played: {user_count}")
print("=" * 50)
for row in rows:
print("=" * 50)
print(f"NAME: {row[1]}")
print(f"Score: {row[2]}")
print(f"Gift: {row[3] if row[3] else 'None'}")
print(f"Additional Cashback Rewarded: {row[4] if row[4] else 'None'}")
print("-" * 50)
print("=" * 50)
# Closing the database connection
cursor.close()
conn.close()