Guest User

Untitled

a guest
Nov 15th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. import random # Imports the random module
  2. def readData(): # Load Files and Log In
  3. with open("Song Names.txt", "r") as f:# Opens the song file
  4. songNames = [line.rstrip('n') for line in f]# Puts the song file into a list
  5. f.close()
  6. with open("Artist Names.txt", "r") as f:# Opens the artists file
  7. artistNames = [line.rstrip('n') for line in f]# Puts the artists file into a list
  8. f.close()
  9. with open("Usernames.txt", "r") as f:# Opens the username file
  10. usernames = [line.rstrip('n') for line in f]# Puts the username file into a list
  11. f.close()
  12. with open("Passwords.txt", "r") as f:# Opens the username file
  13. passwords = [line.rstrip('n') for line in f]# Puts the username file into a list
  14. f.close()
  15. return songNames, artistNames, usernames, passwords # Returns the newly created list
  16.  
  17. def login(usernames, passwords):
  18. global enterUsername
  19. enterUsername = input("Enter usernamen> ")# Prompts the user to enter their username
  20. if enterUsername in usernames: # If the username is in usernames...
  21. enterPassword = str(input("Enter passwordn> ")) # Prompts the user to enter their password
  22. usernamePos = usernames.index(enterUsername) # Finds the position ofthe username
  23. if enterPassword in passwords: # If the password is in passwords...
  24. passwordPos = passwords.index(enterPassword) # Finds the position of the entered password
  25. if usernamePos == passwordPos: # If the password and the username is in the same position
  26. print ("You are logged in", enterUsername) # Log in!
  27. return True
  28. else: # If the password is not in the same position
  29. print("You entered the wrong password...") # Inform the user about their mistake
  30. else: # If the password is not in passwords...
  31. print("That password is not in the file...")# Inform the user about their mistake
  32. else: # If the username is not in usernames
  33. print("You're not allowed to play the game...") # Inform the user about their mistake
  34. return False # Tell the programme that the user did not log in
  35.  
  36. def chooseSong(songNames, artistNames): # Sets up the song requirew
  37. secretSong = random.choice(songNames) # Generates a random song
  38. sSP = songNames.index(secretSong) # Finds the position of the secret song
  39. secretArtist = artistNames[sSP] # Gets the correct artist from the song
  40. return secretSong, secretArtist # Returns the song or artist
  41.  
  42. def loadGame(secretSong, secretArtist): # Loads the game
  43. word_guessed = [] # return secretSong
  44. print ("The artist is ", secretArtist, "and the song is", secretSong) # Print the artists nam
  45. for letter in secretSong: # Prints the song name (censored, of course)
  46. if letter == " ": # If the ltetter is a space...
  47. word_guessed.append(" / ") # Print a dash
  48. else: # Otherwise...
  49. word_guessed.append("-") # Print a line
  50. for letter in word_guessed:
  51. print (letter, end = " ")
  52. return secretSong # Returns the secret song
  53.  
  54. def playGame(secretSong, score): # Plays the game
  55. tries = 0 # Sets the amount of tries to 0
  56. while True:
  57. userGuess = str(input("nWhat is the song name?n> ")) # Gives the user the oppertunity to guess the song name
  58. if userGuess == secretSong: # If the user guesses correctly...
  59. print("Correct!") # Prints that they are correct
  60. if tries == 0: # If they got the song name correct on their first try
  61. score = score + 3 # Add 3 points to thesongNames = [line.rstrip('n') for line in f] score
  62. 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
  63. time.sleep(1) # Wait a second
  64. return score, True # Return the score and True, to run the game again
  65. elif tries == 1: # If they got the song name correct on their second try
  66. score = score + 1 # Add 1 point to the score
  67. 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
  68. time.sleep(1) # Wait a second
  69. return score, True # Return the score and True, to run the game again
  70. else: # If the user guessed the song wrong
  71. tries = tries + 1 # You add a try to the programme
  72. if tries == 2: # while the tries are equal to 2
  73. print ("Game over! You got", score, "points!") # Prints the game over screen, showing the points they earned in the game (if any)
  74. time.sleep(3) # Waits three seconds
  75. return score, False # Returns False, to display the high scores
  76. break # Breaks from the while True loop
  77. print("You have 1 try left.") # Prints that the user has 1 try left
  78. else:
  79. print("Incorrect!") # Tells the user that they were wrong
  80. time.sleep(0.5) # Wait half a second
  81. print("You have 1 try left.") # Prints that the user has 1 try left
  82. def highScore(score, enterUsername):
  83. sTF1 = (enterUsername + " scored ") # Save To File part 1
  84. sTF2 = (str(score + " points.")) # Save To File part 2
  85. sTF = str("n" + sTF1 + sTF2) # Save To File
  86. with open("Scores.txt", "r") as f: # Opens the score file
  87. highScores = [line.rstrip('n') for line in f]
  88. f.close()# Writes the score to the high score file
  89. with open("Scores.txt", "a") as f:
  90. f.write(sTF)
  91. f.close()
  92. openScores = input("Would you like to see the score? Y/Nn> ") # Gives the user the option to view the scores
  93. if openScores.lower() == ("y" or "yes"):
  94. print (highScores)
  95. time.sleep(5)
  96. quit()
  97. else:
  98. quit()
  99.  
  100. def main():
  101. songNames, artistNames, usernames, passwords = readData()# Reads the data from text files
  102. score = 0 # Set the score to 0
  103. success = login(usernames, passwords)# prompts the user to login
  104. if success == True: # If they log in successfully
  105. while True:
  106. print ('''Rules of the game!
  107. 1) The name of the song is in all capitals
  108. 2) There must be no space after your entry
  109. 3) Have fun!''')
  110. secretSong, artist = chooseSong(songNames, artistNames) # Chooses a random song
  111. loadGame(secretSong, artist) # Creates the censored song name
  112. score, win = playGame(secretSong, score) # Plays the game
  113. if win == False: # If you lose the game
  114. highScore(score, enterUsername)
  115. break
  116. main() #Plays the game!
Add Comment
Please, Sign In to add comment