Advertisement
Guest User

Python Quiz Game

a guest
Jan 24th, 2019
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.40 KB | None | 0 0
  1. import sys, os, random, csv
  2.    
  3. def welcome():
  4.     print ("#####################################")
  5.     print ("#  .::Welcome to The Music Quiz::.  #")
  6.     print ("#           .:Version 1:.           #")
  7.     print ("#####################################")
  8.     print ("")
  9.     Choice = input('Do you want to login or register? ')
  10.     Choice = Choice.lower()
  11.  
  12.     if Choice ==  'login':
  13.         login()
  14.     elif Choice == 'register':
  15.         register()
  16.     else:
  17.         print ('Enter login or register')
  18.  
  19. def register():
  20.     print ('Follow the register instructions')
  21.  
  22.     username_check = False
  23.  
  24.     while username_check == False:
  25.        
  26.         username = input('Please enter a Username: ')
  27.  
  28.         with open('users.csv') as csvfile:
  29.             reader = csv.reader(csvfile)
  30.             for row in reader:
  31.                 if username == row[0]:
  32.                     print ('Username not available, try again.')
  33.                     username_check = False
  34.                 else:
  35.                     username_check = True
  36.  
  37.     if username_check == True:
  38.  
  39.         password_check = False
  40.  
  41.         while password_check == False:
  42.             password = input('Please enter a Password: ')
  43.  
  44.             score = "0"
  45.  
  46.             account = username + ',' + password + ',' + score + '\n'
  47.            
  48.             with open('users.csv', "a") as f:
  49.                 for row in account:
  50.                     f.write(row)
  51.            
  52.             print ("Thank You for Registering, You can now login")
  53.             print ("")
  54.             login()
  55.            
  56. def login():
  57.     logged_in = False
  58.    
  59.     while logged_in == False:
  60.         username = input('Please enter your Username ')
  61.         password = input('Please enter your Password ')
  62.        
  63.         database = open('users.csv')
  64.         users = csv.reader(database)
  65.        
  66.         for row in users:
  67.             if username == row[0] and password == row[1]:
  68.                 logged_in = True
  69.                 print ('Logged in successfully as ' + username)
  70.                 print ("")
  71.  
  72.                 database = open('users.csv')
  73.                 users = csv.reader(database)
  74.  
  75.                 for row in users:
  76.                     if username == row[0]:
  77.                         currentScore = row[2]
  78.                         currentScore = int((currentScore))
  79.  
  80.                 song_list = ''
  81.                 song_name = ''
  82.                 artist_name = ''
  83.                 song_letters = ''
  84.                 chances = 2
  85.                 score = 0
  86.                 quiz = True
  87.                
  88.                 with open('songs.csv', 'rt') as f:
  89.                     reader = csv.reader(f)
  90.                     song_list = list(reader)
  91.  
  92.                 print ("Your Highest Score is " + str(currentScore))
  93.                 print ("Can you beat it?")
  94.                 print ("")
  95.                    
  96.                 while quiz == True:
  97.                     if chances == 2:
  98.                         Song = (random.choice(song_list))
  99.                         Song = str(Song)
  100.                         Song.split(",")
  101.                         artist_name,song_name = Song.split(",")
  102.  
  103.                     for char in "']":
  104.                         song_name = song_name.replace(char,'')
  105.  
  106.                     for char in "['":
  107.                         artist_name = artist_name.replace(char,'')
  108.  
  109.                     song_name.lower()
  110.  
  111.                     print ("You Have " + str(chances), "chances remaining!")
  112.                     print ("The Artist of the song is " + artist_name )
  113.  
  114.                     words = song_name.split()
  115.                     song_letters = [word[0] for word in words]
  116.  
  117.                     print ("The first letter of each word in the song name are " + str(song_letters))
  118.                     guess = input ("What is the name of the song? ")
  119.                     guess = guess.lower()
  120.  
  121.                     if guess == song_name:
  122.                         print ("Correct the Song was " + song_name, "by " + artist_name)
  123.                         score = score + 3
  124.                         print ("")
  125.                         print ("You scored 3 points for guessing the song the first try")
  126.  
  127.                     else:
  128.                         print ("That is incorrect, Try again")
  129.                         print ("You have 1 chance left!")
  130.                         chances = chances - 1
  131.  
  132.                         guess = input ("What is the name of the song? ")
  133.                         guess = guess.lower()
  134.  
  135.                         if guess == song_name:
  136.                             print ("Correct the Song was " + song_name, "by " + artist_name)
  137.                             score = score + 1
  138.                             print ("")
  139.                             print ("You scored 1 point for guessing the song the second try")
  140.                             chances = 0
  141.                             chances = 2
  142.  
  143.                         else:
  144.                             print ("")
  145.                             print ("You have run out of lives!")
  146.                             print ("You Scored " + str(score), " points")
  147.  
  148.                             if int(score) > int(currentScore):
  149.                                 currentscore = score
  150.  
  151.                             quiz = False
  152.  
  153.             else:
  154.                 logged_in = False
  155.                 print ('One or more details are incorrect, Try Again')
  156.            
  157. welcome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement