Advertisement
Guest User

11:32 python

a guest
Nov 23rd, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. #this imports the different libraries I will be using
  2. import random
  3. import math
  4. import sys
  5. import time
  6. import webbrowser
  7.  
  8. def loadsongs():
  9. #this creates an empty list, which then has a text file loaded into it
  10. songlist = []
  11. #opens the file
  12. file = open("songs.txt","r")
  13. for line in file:
  14. songlist.append(line)
  15. file.close()
  16. #this loads the next function
  17. print(songlist)
  18. randomsong(songlist)
  19.  
  20. def randomsong(songlist):
  21. #this sets the starting score and number of guesses to 0
  22. score = 0
  23. guesses = 0
  24. while len(songlist) and guesses < 2:
  25. #this selects a random song from the list and puts it into two different variables
  26. choice = random.choice(songlist)
  27. split = choice.find("by")
  28. song = choice[:split]
  29. artist = choice[split:]
  30. songcaps = song.upper()
  31. songcapsletter = songcaps[0]
  32. #this selects a song from the list at random
  33. print("")
  34. print(songcapsletter)
  35. print(artist)
  36. guess = str(input("What is the name of this song?: "))
  37. guesscaps = guess.upper()
  38. if guesscaps == songcaps:
  39. #this adds 3 to the score if the user gets the answer right
  40. score = score + 3
  41. print("Wow! You exist!")
  42. print("")
  43. print("Your current score is",score)
  44. #this takes the correctly guessed song out of the list
  45. songlist.remove(choice)
  46. guesses = 0
  47. else:
  48. guesses = guesses + 1
  49. guess = str(input("Sorry, but our princess is in another castle! guess again: "))
  50. guesscaps = guess.upper()
  51. if songcaps == guesscaps:
  52. print("Second times a charm or whatever")
  53. print("")
  54. score = score + 1
  55. print("Your current score is",score)
  56. songlist.remove(choice)
  57. guesses = 0
  58. else:
  59. print ("game over, your final score was",score)
  60. #this ends the game and adds the score to the leaderboard
  61. guesses = guesses + 1
  62. score = str(score)
  63. file = open("scores.txt","a+")
  64. file.write(username)
  65. file.write(",")
  66. file.write(score)
  67. file.write("\n")
  68. file.close()
  69. leaderboard()
  70.  
  71. def leaderboard():
  72. scoreslist = []
  73. file = open("scores.txt")
  74. for line in file:
  75. splits = line.split(",")
  76. points = splits[1]
  77. name = splits[0]
  78. points = int(points)
  79. scoreslist.append([points,name])
  80. for i in range(5):
  81. maximumscore = max(scoreslist)
  82. place = i + 1
  83. maxname = maximumscore[1]
  84. maxscore = maximumscore[0]
  85. scoreslist.remove(maximumscore)
  86. print("In position number",place,"is",maxname,"with a score of",maxscore)
  87. time.sleep(1)
  88. file.close()
  89. time.sleep(5)
  90. mainmenu()
  91.  
  92. def instructions():
  93. #this prints the instructions when the menu option is selected
  94. print("""in this game, a random song name and artist are chosen.
  95. the artist and first letter of each word in the song title are then displayed.
  96. you have 2 chances to guess the song.
  97. answering correctly on your first try gives you 3 points.
  98. if its guesses correctly the second time, you get 1 point.
  99. the game ends when you get the song incorrect the second time.
  100.  
  101. enter 'close' to exit""")
  102. #this prints an ASCII image of a cake if "cake" is entered on the instruction menu
  103. eastereggcommand = str(input())
  104. if eastereggcommand == "cake":
  105. asciicake = open("cake.txt", "r")
  106. print(asciicake.read())
  107. asciicake.close
  108. time.sleep(3)
  109. mainmenu()
  110. if eastereggcommand == ("close"):
  111. mainmenu()
  112.  
  113. def mainmenu():
  114. print("""< : : : : : WELCOME TO THE MUSIC GAME : : : : : >
  115. Please input your menu choice:
  116. 1. PLAY GAME
  117. 2. LEADERBOARD
  118. 3. INSTRUCTIONS
  119. 4. EXIT
  120. 5. BUY BATTLE PASS""")
  121. #user input for choice
  122. switch = False
  123. while switch ==False:
  124. while True:
  125. try:
  126. choice = int(input())
  127. if choice == 1:
  128. loadsongs()
  129. if choice == 2:
  130. leaderboard()
  131. if choice == 3:
  132. instructions()
  133. if choice == 4:
  134. exit()
  135. if choice == 5:
  136. webbrowser.open_new("https://www.paladins.com/battle-pass")
  137. break
  138. except ValueError:
  139. print ("not a number")
  140.  
  141. def choose():
  142. #this asks the user if they want to signup or login
  143. print("Welcome! Would you like to login (1) or signup (2)?: ")
  144. choice = int(input())
  145. if choice == 1:
  146. #redirection to the login function
  147. login()
  148. if choice == 2:
  149. #redirection to the signup function
  150. signup()
  151.  
  152. def signup():
  153. #this code asks the user to enter new information, which is then put into a text file and read from whenever the user logs in
  154. newUsername = str(input("Please enter a new username: "))
  155. newPassword = str(input("Please enter a new password: "))
  156. info = open("info.txt","a+")
  157. info.write(newUsername)
  158. info.write(newPassword)
  159. info.write("\n")
  160. print("Thank you for creating a new account")
  161. print("You will now be redirected to the login screen")
  162. info.close()
  163. login()
  164.  
  165. def login():
  166. #this asks the user to input the username and password
  167. file = open("info.txt","r")
  168. global username
  169. username = str(input("Please input your username: "))
  170. password = str(input("Please input your password: "))
  171. combined = username+password
  172. #this allows the user access if their name and password are in the file, and repeats the login() function if they input incorrect info
  173. for line in file:
  174. if combined in line:
  175. print ("access granted")
  176. file.close()
  177. mainmenu()
  178. if combined not in file:
  179. print("access denied")
  180. file.close()
  181. login()
  182.  
  183. choose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement