Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###NEA Programming Project June 2019 Task 1###
- import csv
- import time #Import modules
- import random
- userbaseList = []
- loginAttempts = 0
- musicList = []
- musicFile = open("music.csv", mode="r") #open music.csv
- musicFileCompiler = csv.reader(musicFile)
- for row in musicFileCompiler:
- musicList.append(row)
- def logIn():
- userbaseData = open("userbase.csv", mode="r") #open and read userbase
- userbaseReader = csv.reader(userbaseData)
- loggedIn = False
- for row in userbaseReader: #loads userbase into a useable list
- userbaseList.append(row)
- userbaseData.close()
- print ("What's your username?")
- username = input("")
- usernameFound = False
- for row in userbaseList:
- print (row)#repeats over every username-password combo
- if username == row[0]: #if username exists, usernameFound = True, so the program knows that the account exists
- print ("Username found.")
- usernameFound = True
- while loggedIn == False: #repeats until user gets password right
- print ("What's your password?")
- password = input("")
- if password == row[1]: #passwords are always saved at row[1] when signing up
- print ("Logged in")
- loggedIn = True
- else: #if the password doesn't match the username, the while loop loops again
- print ("Password incorrect. Please try again.")
- if usernameFound == False: #if the usename isn't found, they're prompted to sign up
- print ("Username does not exist")
- signUp(username)
- else:
- print ("Welcome")
- gameSystem(username)
- def signUp(username):
- print ("Would you like to sign up?")
- validInput = False
- while validInput == False:
- signUpRequest = input("")
- if signUpRequest.upper() == "Y" or signUpRequest.upper() == "YES":
- validInput = True
- print ("Signing up with account name",username)
- password = "" #creates empty variable
- passwordCheck = None
- while password != passwordCheck : #repeats until their entered password is the same as what they entered
- print ("What's your password?")
- password = input("")
- print ("Retype your password")
- passwordCheck = input("") #fetches password and password check
- if password != passwordCheck: #compares passwords
- print ("These passwords don't match up. Please try again.")
- else:
- print ("Passwords match.")
- print ("Account",username,"created.")
- newProfile = [username, password] #creates profile
- userbaseList.append(newProfile) #adds profile to user base list
- userbaseData = open("userbase.csv", mode ="w", newline = "") #sets up user base csv file
- userbaseWriter = csv.writer(userbaseData) #sets up file writer
- for row in userbaseList: #writes each profile contained in userbaseList to userbase csv file
- userbaseWriter.writerow(row)
- userbaseData.close()
- gameSystem(username) #begins game, passing the username parameter
- elif signUpRequest.upper() == "N" or signUpRequest.upper() == "NO":
- validInput = True
- print ("Redirecting to Login.")
- logIn()
- else:
- print ("Please give a valid input.")
- def Leaderboard(username, points): #runs the leaderboard using the username and no. of points provided when called in the gameSystem()
- leaderboardList = []
- leaderboardFile = open("Leaderboard.csv", mode = "r") #generates list containing all of the current leaderboard
- leaderboardData = csv.reader(leaderboardFile)
- for row in leaderboardData:
- leaderboardList.append(row)
- leaderboardFile.close()
- leaderboardList.append([username,points]) #adds the new user's submission to the leaderboardList, including username and no. of points
- leaderboardList.sort(key=lambda x: int(x[1])) #organises the 2d array according to index 1 of each within each column (the points)
- leaderboardList.reverse() #reverses the list so that the leaderboard is saved in descending order
- leaderboardFile = open("Leaderboard.csv",mode = "w", newline = "")
- leaderboardWriter = csv.writer(leaderboardFile)
- for row in leaderboardList: #writes the leaderboardList to the leader board csv file
- leaderboardWriter.writerow(row)
- print ("User -- Points Scored")
- leaderboardPosition = 0 #as the "range" function cant be used here, incrementing is done manually here
- index = 0
- for row in leaderboardList:
- leaderboardPosition += 1
- print (str(leaderboardPosition)+":",leaderboardList[index][0],"with",leaderboardList[index][1],"points") #formats and prints the leaderboard, including position, name and then no. og points
- index += 1
- def gameSystem(username): #runs game, using username parameter passed from signUp() or logIn()
- username = username
- gameOver = False #untroduction
- 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
- time.sleep(5)
- print ("Get it on your second attempt, and you'll earn 1 point.")
- time.sleep(4)
- print ("If you don't guess it correctly on the second attempt, game over!")
- time.sleep(2)
- print ("Ready?")
- time.sleep(1)
- print ("Set!")
- time.sleep(1)
- print ("Go!")
- time.sleep(1)
- points = 0
- while gameOver == False: #repeats until the user fails
- arrayIndex = random.randint(0, len(musicList) - 1) #Randomly Selects an array
- array = musicList[arrayIndex] #loads full array
- band = array[0] #fetches band name from array
- songIndex = random.randint(1, len(array) - 1) #randomly selects song index from array
- song = array[songIndex] #fetches song
- songWords = song.split() #seperates each word of the song name into a list
- songInitialsList = [word[0] for word in songWords] #places the first letter of each word into a list
- songInitials = ("".join(songInitialsList))#joins the entire list, generating the song initials
- attempts = 0
- leaveLoop = False
- while leaveLoop != True:
- 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
- print ("Name the song by",band,"with the initials '",songInitials,"'") #prints message
- guess = input("") #fetches user's guess
- if guess.upper() == song.upper(): #capitalises both variables to provide clarity and case insensitivity
- print ("Correct")
- if attempts == 0: #if they get it on their first attempt, they earn 3 points
- print ("+3 points!")
- points = points + 3
- print ("You now have",points,"points!")
- 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
- print ("+1 points")
- points = points + 1
- print ("You now have",points,"points!")
- leaveLoop = True #breaks from the loop, restarting the guessing process
- else:
- print ("Incorrect")
- attempts = attempts + 1
- else:
- leaveLoop = True #allows to break from loop
- if attempts == 2:
- print ("Gameover! The correct answer was " + song) #fail message
- 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
- print ("You got",points,"points!") #final message
- Leaderboard(username, points) #runs leaderboard(), passing the username and points parameters
- logIn()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement