Advertisement
Guest User

NEA

a guest
Nov 19th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.42 KB | None | 0 0
  1. ###NEA Programming Project June 2019 Task 1###
  2.  
  3. import csv
  4. import time #Import modules
  5. import random
  6.  
  7. userbaseList = []
  8. loginAttempts = 0
  9. musicList = []
  10.  
  11. musicFile = open("music.csv", mode="r") #open music.csv
  12. musicFileCompiler = csv.reader(musicFile)
  13.  
  14. for row in musicFileCompiler:
  15.     musicList.append(row)
  16.    
  17. def logIn():
  18.     userbaseData = open("userbase.csv", mode="r") #open and read userbase
  19.     userbaseReader = csv.reader(userbaseData)
  20.  
  21.     loggedIn = False
  22.    
  23.     for row in userbaseReader: #loads userbase into a useable list
  24.         userbaseList.append(row)
  25.  
  26.     userbaseData.close()
  27.    
  28.     print ("What's your username?")
  29.     username = input("")
  30.  
  31.     usernameFound = False
  32.     for row in userbaseList:
  33.         print (row)#repeats over every username-password combo
  34.         if username == row[0]: #if username exists, usernameFound = True, so the program knows that the account exists
  35.             print ("Username found.")
  36.             usernameFound = True
  37.             while loggedIn == False: #repeats until user gets password right
  38.                 print ("What's your password?")
  39.                 password = input("")
  40.                 if password == row[1]: #passwords are always saved at row[1] when signing up
  41.                     print ("Logged in")
  42.                     loggedIn = True
  43.                 else: #if the password doesn't match the username, the while loop loops again
  44.                     print ("Password incorrect. Please try again.")
  45.     if usernameFound == False: #if the usename isn't found, they're prompted to sign up
  46.         print ("Username does not exist")
  47.         signUp(username)
  48.     else:
  49.         print ("Welcome")
  50.         gameSystem(username)
  51.  
  52. def signUp(username):
  53.     print ("Would you like to sign up?")
  54.     validInput = False
  55.     while validInput == False:
  56.         signUpRequest =  input("")
  57.         if signUpRequest.upper() == "Y" or signUpRequest.upper() == "YES":
  58.             validInput = True
  59.             print ("Signing up with account name",username)
  60.             password = "" #creates empty variable
  61.             passwordCheck = None
  62.             while password != passwordCheck : #repeats until their entered password is the same as what they entered
  63.                    print ("What's your password?")
  64.                    password = input("")
  65.                    print ("Retype your password")
  66.                    passwordCheck = input("") #fetches password and password check
  67.                    if password != passwordCheck: #compares passwords
  68.                        print ("These passwords don't match up. Please try again.")
  69.                    else:
  70.                        print ("Passwords match.")
  71.                        print ("Account",username,"created.")
  72.                        newProfile = [username, password] #creates profile
  73.                        userbaseList.append(newProfile) #adds profile to user base list
  74.                          
  75.                        userbaseData = open("userbase.csv", mode ="w", newline = "") #sets up user base csv file
  76.                        userbaseWriter = csv.writer(userbaseData) #sets up file writer
  77.                        for row in userbaseList: #writes each profile contained in userbaseList to userbase csv file
  78.                            userbaseWriter.writerow(row)
  79.  
  80.                        userbaseData.close()
  81.                        gameSystem(username) #begins game, passing the username parameter
  82.         elif signUpRequest.upper() == "N" or signUpRequest.upper() == "NO":
  83.             validInput = True
  84.             print ("Redirecting to Login.")
  85.             logIn()
  86.         else:
  87.             print ("Please give a valid input.")
  88.  
  89. def Leaderboard(username, points): #runs the leaderboard using the username and no. of points provided when called in the gameSystem()
  90.     leaderboardList = []
  91.    
  92.     leaderboardFile = open("Leaderboard.csv", mode = "r") #generates list containing all of the current leaderboard
  93.     leaderboardData = csv.reader(leaderboardFile)
  94.     for row in leaderboardData:
  95.         leaderboardList.append(row)
  96.  
  97.     leaderboardFile.close()
  98.    
  99.     leaderboardList.append([username,points]) #adds the new user's submission to the leaderboardList, including username and no. of points
  100.     leaderboardList.sort(key=lambda x: int(x[1])) #organises the 2d array according to index 1 of each within each column (the points)
  101.     leaderboardList.reverse() #reverses the list so that the leaderboard is saved in descending order
  102.  
  103.     leaderboardFile = open("Leaderboard.csv",mode = "w", newline = "")
  104.     leaderboardWriter = csv.writer(leaderboardFile)
  105.     for row in leaderboardList: #writes the leaderboardList to the leader board csv file
  106.         leaderboardWriter.writerow(row)
  107.    
  108.     print ("User -- Points Scored")
  109.     leaderboardPosition = 0 #as the "range" function cant be used here, incrementing is done manually here
  110.     index = 0
  111.     for row in leaderboardList:
  112.         leaderboardPosition += 1
  113.         print (str(leaderboardPosition)+":",leaderboardList[index][0],"with",leaderboardList[index][1],"points") #formats and prints the leaderboard, including position, name and then no. og points
  114.         index += 1
  115. def gameSystem(username): #runs game, using username parameter passed from signUp() or logIn()
  116.    
  117.     username = username    
  118.  
  119.     gameOver = False #untroduction
  120.     print ("I will display an Artist's name, and then the initials of one of their songs. Guess that song (comma and apostrophe specific!) correctly on the first attempt, and you will earn 3 points. ") #Intro
  121.     time.sleep(5)
  122.     print ("Get it on your second attempt, and you'll earn 1 point.")
  123.     time.sleep(4)
  124.     print ("If you don't guess it correctly on the second attempt, game over!")
  125.     time.sleep(2)
  126.     print ("Ready?")
  127.     time.sleep(1)
  128.     print ("Set!")
  129.     time.sleep(1)
  130.     print ("Go!")
  131.     time.sleep(1)
  132.  
  133.     points = 0
  134.     while gameOver == False: #repeats until the user fails
  135.         arrayIndex = random.randint(0, len(musicList) - 1) #Randomly Selects an array
  136.         array = musicList[arrayIndex] #loads full array
  137.         band = array[0] #fetches band name from array
  138.         songIndex = random.randint(1, len(array) - 1) #randomly selects song index from array
  139.         song = array[songIndex] #fetches song
  140.  
  141.         songWords = song.split() #seperates each word of the song name into a list
  142.         songInitialsList = [word[0] for word in songWords] #places the first letter of each word into a list
  143.         songInitials = ("".join(songInitialsList))#joins the entire list, generating the song initials
  144.  
  145.         attempts = 0
  146.         leaveLoop = False
  147.         while leaveLoop != True:
  148.             if attempts != 2: #checks no. of attempts before running. if it is 2 or more the loop doesnt run, therefore the user can't guess again and it breaks from the loop
  149.                 print ("Name the song by",band,"with the initials '",songInitials,"'") #prints message
  150.                 guess = input("") #fetches user's guess
  151.                 if guess.upper() == song.upper(): #capitalises both variables to provide clarity and case insensitivity
  152.                     print ("Correct")
  153.                     if attempts == 0: #if they get it on their first attempt, they earn 3 points
  154.                         print ("+3 points!")
  155.                         points = points + 3
  156.                         print ("You now have",points,"points!")
  157.                     elif attempts == 1: #if they get it on their second attempt, they earn 1 point. there is no "elif attempts == 2:" as the loop wouldn't run at all if that condition were true
  158.                         print ("+1 points")
  159.                         points = points + 1
  160.                         print ("You now have",points,"points!")
  161.                     leaveLoop = True #breaks from the loop, restarting the guessing process
  162.                 else:
  163.                     print ("Incorrect")
  164.                     attempts = attempts + 1
  165.             else:
  166.                 leaveLoop = True #allows to break from loop
  167.  
  168.         if attempts == 2:
  169.             print ("Gameover! The correct answer was " + song) #fail message
  170.             gameOver = True #once the loop is broken from, no. of attempts is checked. it attempts == 2, thje program knows that the loop was broken out of because the user failed
  171.    
  172.     print ("You got",points,"points!") #final message
  173.     Leaderboard(username, points) #runs leaderboard(), passing the username and points parameters
  174.  
  175. logIn()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement