Advertisement
Jhynjhiruu

GCSE Computer Science Example NEA code

Jan 29th, 2019
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import random
  2.  
  3. try:
  4.     rawSongs = open("songs.txt").readlines()
  5.     rawUsers = open("users.txt").readlines()
  6.     rawHighscores = open("highscores.txt").readlines()
  7. except:
  8.     print("At least one file is missing! Make sure songs.txt, users.txt and highscores.txt are all present in the working directory before running this program.")
  9.     raise SystemExit
  10.  
  11. songs = []
  12. for i in rawSongs:
  13.     fields = i.split(",")
  14.     title = fields[0]
  15.     artist = fields[1].rstrip("\n")
  16.     songs.append([title, artist])
  17.  
  18. users = []
  19. for i in rawUsers:
  20.     fields = i.split(",")
  21.     username = fields[0]
  22.     password = fields[1].rstrip("\n")
  23.     users.append([username, password])
  24.  
  25. highscores = []
  26. for i in rawHighscores:
  27.     fields = i.split(",")
  28.     score = int(fields[0])
  29.     name = fields[1].rstrip("\n")
  30.     highscores.append([score, name])
  31.  
  32. # User validation
  33.  
  34. userDetails = None
  35.  
  36. while True:
  37.     userInput = input("Please enter your username:\n")
  38.     for i in users:
  39.         if i[0] == userInput:
  40.             userDetails = i
  41.             break
  42.     else:
  43.         print("Username not found!")
  44.         continue
  45.     break
  46.  
  47. while True:
  48.     passInput = input("Please enter your password:\n")
  49.     if passInput == userDetails[1]:
  50.         print("Successfully logged in as {}!".format(userDetails[0]))
  51.         break
  52.     else:
  53.         print("Incorrect password!")
  54.  
  55. # Main game loop
  56.  
  57. usedSongs = []
  58.  
  59. curSong = None
  60.  
  61. allSongsUsed = False
  62.  
  63. userScore = 0
  64.  
  65. while True:
  66.     while True:
  67.         temp = random.choice(songs)
  68.         if temp not in usedSongs:
  69.             usedSongs.append(temp)
  70.             curSong = temp
  71.             break
  72.         elif len(usedSongs) == len(songs):
  73.             print("All songs used!")
  74.             allSongsUsed = True
  75.             break
  76.     if allSongsUsed:
  77.         break
  78.    
  79.     initials = []
  80.    
  81.     for i in curSong[0].split():
  82.         initials.append(i[0] + ("_" * (len(i) - 1)))
  83.    
  84.     scoreAdd = 3
  85.    
  86.     while True:
  87.        
  88.         print("Song: {} by {}".format(" ".join(initials), curSong[1]))
  89.        
  90.         guess = input("Please enter your guess at the song title:\n")
  91.        
  92.         if guess.upper() == curSong[0].upper():
  93.             print("Correct!")
  94.             userScore += scoreAdd
  95.             print("Your score is now {}".format(userScore))
  96.             break
  97.         else:
  98.            
  99.             if scoreAdd == 3:
  100.                 print("Incorrect, please try again")
  101.                 scoreAdd = 1
  102.             else:
  103.                 print("Game over! The correct answer was {}".format(curSong[0]))
  104.                 scoreAdd = 0
  105.                 print("Your score: {}".format(userScore))
  106.                 break
  107.     if scoreAdd == 0:
  108.         break
  109.  
  110. highscores.append([userScore, userDetails[0]])
  111.  
  112. sortedHighscores = sorted(highscores, key = lambda x: x[0], reverse = True)
  113.  
  114. print("Highscores:")
  115.  
  116. if len(sortedHighscores) >= 5:
  117.     for i in sortedHighscores[0:5]:
  118.         print("{} by {}".format(*i))
  119. else:
  120.     for i in sortedHighscores:
  121.         print("{} by {}".format(*i))
  122.  
  123. newRawHighscores = []
  124.  
  125. for i in sortedHighscores:
  126.     formatted = [str(i[0]), i[1]]
  127.     new = ",".join(formatted)
  128.     newRawHighscores.append(new)
  129.  
  130. newToWrite = "\n".join(newRawHighscores)
  131. newHighscores = open("highscores.txt", "w")
  132. newHighscores.write(newToWrite)
  133. newHighscores.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement