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