Advertisement
Guest User

Python Music Quiz (Sqlite3 Error)

a guest
Jan 29th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.67 KB | None | 0 0
  1. import csv
  2. import sqlite3
  3. from random import choice
  4.  
  5. connection = sqlite3.connect('users.db')
  6.  
  7. cursor = connection.cursor()
  8.  
  9.  
  10. def welcome():
  11.     print("#####################################")
  12.     print("#  .::Welcome to The Music Quiz::.  #")
  13.     print("#           .:Version 1:.           #")
  14.     print("#####################################")
  15.     print("")
  16.  
  17.     option = input('Do you want to login or register? ')
  18.     option = option.lower()
  19.  
  20.     if option == 'login':
  21.         login()
  22.     elif option == 'register':
  23.         register()
  24.     else:
  25.         print('Enter login or register')
  26.  
  27.  
  28. def register():
  29.  
  30.     sql = '''CREATE TABLE IF NOT EXISTS Players
  31.                    (PID INTEGER PRIMARY KEY AUTOINCREMENT,
  32.                     USERNAME VARCHAR(100),
  33.                     PASSWORD VARCHAR(100),
  34.                     SCORE INT)'''
  35.     cursor.execute(sql)
  36.  
  37.     print('Follow the register instructions')
  38.  
  39.     username_check = False
  40.  
  41.     while username_check == False:
  42.  
  43.         tempusername = input('Please enter a Username: ')
  44.  
  45.         cursor.execute('''SELECT username FROM Players WHERE username=?''', (tempusername,))
  46.         exists = cursor.fetchall()
  47.  
  48.         if not exists:
  49.             username_check = True
  50.         else:
  51.             print('Username not available, try again.')
  52.             username_check = False
  53.  
  54.     if username_check == True:
  55.  
  56.         password_check = False
  57.  
  58.         while password_check == False:
  59.             temppassword = input('Please enter a Password: ')
  60.  
  61.             tempscore = "0"
  62.  
  63.             params = (tempusername, temppassword, tempscore)
  64.  
  65.             cursor.execute("INSERT INTO Players VALUES (NULL, ?, ?, ?)", params)
  66.             cursor.execute(sql)
  67.  
  68.             connection.commit()
  69.  
  70.             print("Thank You for Registering, You can now login")
  71.             print("")
  72.             login()
  73.  
  74.  
  75. def login():
  76.     logged_in = False
  77.  
  78.     while logged_in == False:
  79.         username = input('Please enter your Username ')
  80.         password = input('Please enter your Password ')
  81.  
  82.         cursor.execute('SELECT * from Players WHERE username="%s" AND password="%s"' % (username, password))
  83.  
  84.         if cursor.fetchone() is not None:
  85.             logged_in = True
  86.             print('Logged in successfully as ' + username)
  87.             print("")
  88.  
  89.             cursor.execute('SELECT score from Players WHERE username="%s"' % (username))
  90.             currentScore = cursor.fetchall()
  91.             currentScore = [x[0] for x in currentScore]
  92.  
  93.             song_list = ''
  94.             song_name = ''
  95.             artist_name = ''
  96.             song_letters = ''
  97.             chances = 2
  98.             score = 0
  99.             quiz = True
  100.  
  101.             with open('songs.csv', 'rt') as f:
  102.                 reader = csv.reader(f)
  103.                 song_list = list(reader)
  104.  
  105.                 print("Your Highest Score is " + str(currentScore))
  106.                 print("Can you beat it?")
  107.                 print("")
  108.  
  109.                 while quiz == True:
  110.                     if chances == 2:
  111.                         Song = (choice(song_list))
  112.                         Song = str(Song)
  113.                         Song.split(",")
  114.                         artist_name, song_name = Song.split(",")
  115.  
  116.                     for char in "']":
  117.                         song_name = song_name.replace(char, '')
  118.  
  119.                     for char in "['":
  120.                         artist_name = artist_name.replace(char, '')
  121.  
  122.                     song_name.lower()
  123.  
  124.                     print("You Have " + str(chances), "chances remaining!")
  125.                     print("The Artist of the song is " + artist_name)
  126.  
  127.                     words = song_name.split()
  128.                     song_letters = [word[0] for word in words]
  129.  
  130.                     print("The first letter of each word in the song name are " + str(song_letters))
  131.                     guess = input("What is the name of the song? ")
  132.                     guess = guess.lower()
  133.  
  134.                     if guess == song_name:
  135.                         print("Correct the Song was " + song_name, "by " + artist_name)
  136.                         score = score + 3
  137.                         print("")
  138.                         print("You scored 3 points for guessing the song the first try")
  139.  
  140.                     else:
  141.                         print("That is incorrect, Try again")
  142.                         print("You have 1 chance left!")
  143.                         chances = chances - 1
  144.  
  145.                         guess = input("What is the name of the song? ")
  146.                         guess = guess.lower()
  147.  
  148.                         if guess == song_name:
  149.                             print("Correct the Song was " + song_name, "by " + artist_name)
  150.                             score = score + 1
  151.                             print("")
  152.                             print("You scored 1 point for guessing the song the second try")
  153.                             chances = 0
  154.                             chances = 2
  155.  
  156.                         else:
  157.                             print("")
  158.                             print("You have run out of lives!")
  159.                             print("You Scored " + str(score), "points")
  160.  
  161.                             if str((score)) > str((currentScore)):
  162.                                 currentscore = score
  163.  
  164.                                 cursor.execute('''UPDATE Players SET score = ? WHERE username =?''', (username, score))
  165.                                 connection.commit()
  166.                                 connection.close()
  167.  
  168.                             quiz = False
  169.  
  170.         else:
  171.             logged_in = False
  172.             print('One or more details are incorrect, Try Again')
  173.  
  174.  
  175. welcome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement