Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random # Imports the random module
- def readData(): # Load Files and Log In
- with open("Song Names.txt", "r") as f:# Opens the song file
- songNames = [line.rstrip('n') for line in f]# Puts the song file into a list
- f.close()
- with open("Artist Names.txt", "r") as f:# Opens the artists file
- artistNames = [line.rstrip('n') for line in f]# Puts the artists file into a list
- f.close()
- with open("Usernames.txt", "r") as f:# Opens the username file
- usernames = [line.rstrip('n') for line in f]# Puts the username file into a list
- f.close()
- with open("Passwords.txt", "r") as f:# Opens the username file
- passwords = [line.rstrip('n') for line in f]# Puts the username file into a list
- f.close()
- return songNames, artistNames, usernames, passwords # Returns the newly created list
- def login(usernames, passwords):
- global enterUsername
- enterUsername = input("Enter usernamen> ")# Prompts the user to enter their username
- if enterUsername in usernames: # If the username is in usernames...
- enterPassword = str(input("Enter passwordn> ")) # Prompts the user to enter their password
- usernamePos = usernames.index(enterUsername) # Finds the position ofthe username
- if enterPassword in passwords: # If the password is in passwords...
- passwordPos = passwords.index(enterPassword) # Finds the position of the entered password
- if usernamePos == passwordPos: # If the password and the username is in the same position
- print ("You are logged in", enterUsername) # Log in!
- return True
- else: # If the password is not in the same position
- print("You entered the wrong password...") # Inform the user about their mistake
- else: # If the password is not in passwords...
- print("That password is not in the file...")# Inform the user about their mistake
- else: # If the username is not in usernames
- print("You're not allowed to play the game...") # Inform the user about their mistake
- return False # Tell the programme that the user did not log in
- def chooseSong(songNames, artistNames): # Sets up the song requirew
- secretSong = random.choice(songNames) # Generates a random song
- sSP = songNames.index(secretSong) # Finds the position of the secret song
- secretArtist = artistNames[sSP] # Gets the correct artist from the song
- return secretSong, secretArtist # Returns the song or artist
- def loadGame(secretSong, secretArtist): # Loads the game
- word_guessed = [] # return secretSong
- print ("The artist is ", secretArtist, "and the song is", secretSong) # Print the artists nam
- for letter in secretSong: # Prints the song name (censored, of course)
- if letter == " ": # If the ltetter is a space...
- word_guessed.append(" / ") # Print a dash
- else: # Otherwise...
- word_guessed.append("-") # Print a line
- for letter in word_guessed:
- print (letter, end = " ")
- return secretSong # Returns the secret song
- def playGame(secretSong, score): # Plays the game
- tries = 0 # Sets the amount of tries to 0
- while True:
- userGuess = str(input("nWhat is the song name?n> ")) # Gives the user the oppertunity to guess the song name
- if userGuess == secretSong: # If the user guesses correctly...
- print("Correct!") # Prints that they are correct
- if tries == 0: # If they got the song name correct on their first try
- score = score + 3 # Add 3 points to thesongNames = [line.rstrip('n') for line in f] score
- print("You earnt 3 points! You have", score, "points in total.") # States that the user has got 3 points, and states the amount of points the user has in total
- time.sleep(1) # Wait a second
- return score, True # Return the score and True, to run the game again
- elif tries == 1: # If they got the song name correct on their second try
- score = score + 1 # Add 1 point to the score
- print("You earnt 1 point! You have", score, "points in total.") # States that the user has got 1 point, and states the amount of points the user has in total
- time.sleep(1) # Wait a second
- return score, True # Return the score and True, to run the game again
- else: # If the user guessed the song wrong
- tries = tries + 1 # You add a try to the programme
- if tries == 2: # while the tries are equal to 2
- print ("Game over! You got", score, "points!") # Prints the game over screen, showing the points they earned in the game (if any)
- time.sleep(3) # Waits three seconds
- return score, False # Returns False, to display the high scores
- break # Breaks from the while True loop
- print("You have 1 try left.") # Prints that the user has 1 try left
- else:
- print("Incorrect!") # Tells the user that they were wrong
- time.sleep(0.5) # Wait half a second
- print("You have 1 try left.") # Prints that the user has 1 try left
- def highScore(score, enterUsername):
- sTF1 = (enterUsername + " scored ") # Save To File part 1
- sTF2 = (str(score + " points.")) # Save To File part 2
- sTF = str("n" + sTF1 + sTF2) # Save To File
- with open("Scores.txt", "r") as f: # Opens the score file
- highScores = [line.rstrip('n') for line in f]
- f.close()# Writes the score to the high score file
- with open("Scores.txt", "a") as f:
- f.write(sTF)
- f.close()
- openScores = input("Would you like to see the score? Y/Nn> ") # Gives the user the option to view the scores
- if openScores.lower() == ("y" or "yes"):
- print (highScores)
- time.sleep(5)
- quit()
- else:
- quit()
- def main():
- songNames, artistNames, usernames, passwords = readData()# Reads the data from text files
- score = 0 # Set the score to 0
- success = login(usernames, passwords)# prompts the user to login
- if success == True: # If they log in successfully
- while True:
- print ('''Rules of the game!
- 1) The name of the song is in all capitals
- 2) There must be no space after your entry
- 3) Have fun!''')
- secretSong, artist = chooseSong(songNames, artistNames) # Chooses a random song
- loadGame(secretSong, artist) # Creates the censored song name
- score, win = playGame(secretSong, score) # Plays the game
- if win == False: # If you lose the game
- highScore(score, enterUsername)
- break
- main() #Plays the game!
Add Comment
Please, Sign In to add comment