· 6 years ago · Dec 20, 2019, 10:50 AM
1##Imports the modules needed to create sql tables and pick random lines from a text document
2import random
3import sqlite3
4
5
6loginUsername = " "
7points = 0
8##Creates the sql table to store logins
9with sqlite3.connect ("Music_Quiz_Game.db") as db:
10 cursor = db.cursor()
11cursor.execute("""
12CREATE TABLE IF NOT EXISTS user(
13userID INTEGER PRIMARY KEY,
14username VARCHAR(20) NOT NULL,
15firstname VARCHAR(20) NOT NULL,
16surname VARCHAR(20) NOT NULL,
17password VARCHAR(20) NOT NULL);
18""")
19
20song = ["gods plan","rockstar","one kiss","havana","girls like you","no rules","lucid dreams","moonlight"]
21
22def newUser():
23 print("Add a new user")
24 found = 0
25 while found == 0:
26 username = input("Please enter a username")
27 with sqlite3.connect("Music_Quiz_Game.db") as db:
28 cursor = db.cursor()
29 find_user = ("SELECT * FROM user WHERE username = ?")
30 cursor.execute(find_user,[(username)])
31
32 if cursor.fetchall():
33 print("Username taken")
34 else:
35 found=1
36 firstname = input("Please enter your firstname")
37 surname = input("Please enter your surname")
38 password = input("Please enter your password")
39 password1 = input("Please enter your password again")
40 while password != password1:
41 print("The passwords did not match please enter them again")
42 password = input("Please enter your password")
43 password1 = input("Please enter your password again")
44
45 insertData = """INSERT INTO user(username,firstname,surname,password)
46 VALUES(?,?,?,?)"""
47 cursor.execute(insertData,[(username),(firstname),(surname),(password)])
48 db.commit()
49
50def login():
51 while True:
52 global loginUsername
53 loginUsername = input("Enter your username")
54 password = input("Enter your password")
55
56 with sqlite3.connect("Music_Quiz_Game.db") as db:
57 cursor = db.cursor()
58 find_user = ("SELECT * FROM user WHERE username = ? AND password = ?")
59 cursor.execute(find_user, [(loginUsername),(password)])
60 results = cursor.fetchall()
61
62 if results:
63 for i in results:
64 print("Welcome")
65 game()
66
67 else:
68 print("Username not recognised")
69 again = input("Do you want to retry? Y/N ")
70 if again == "y" or "Y":
71 login()
72 return loginUsername
73
74def game():
75 global points
76 games = True
77 points = 0
78 while games == True:
79 file = open("songs.txt", "r")
80 words = []
81 for word in file:
82 words.append(word)
83 word = word.strip()
84 word = random.choice(words)
85 print(word)
86 guess1 = input("guess the song")
87 if guess1 in song:
88 print("well done")
89 points = points + 3
90 else:
91 print("You got it wrong, have another go")
92 guess2 = input("guess the song")
93 if guess2 in song:
94 print("well done")
95 points = points + 1
96 else:
97 print("You have failed")
98 games = False
99 scoreBoard()
100
101def scoreBoard():
102 with sqlite3.connect("Scores.db") as db:
103 cursor = db.cursor()
104 cursor.execute("""
105 CREATE TABLE IF NOT EXISTS scores(
106 userID INTEGER PRIMARY KEY,
107 username VARCHAR(50) NOT NULL,
108 points INTEGER NOT NULL);
109 """)
110 insertScore = ("""INSERT INTO score(username,points)
111 VALUES(?,?)""")
112 cursor.execute(insertScore,[(loginUsername),(points)])
113 db.commit()
114 print("The top 3 users are: ")
115 for row in cursor.execute("""SELECT * FROM score ORDER BY points DESC LIMIT 3"""):
116 username = row[1]
117 score = row[2]
118 print(username,":",score)
119 db.close()
120 menu()
121
122def menu():
123 while True:
124
125 menu = ("""
126 Welcome to my music quiz
127 1 - Create a new user
128 2 - Login
129 3 - Exit\n""")
130
131 userChoice = input(menu)
132
133 if userChoice == "1":
134 newUser()
135 elif userChoice == "2":
136 login()
137 elif userChoice == "3":
138 print("Goodbye")
139 break
140 else:
141 print("Input not recognised, please try again")
142
143menu()