Advertisement
Guest User

Untitled

a guest
Nov 7th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. import random,csv
  2. with open("Songs.csv","r",newline='') as csvfile:
  3.     songs = [i for i in csv.reader(csvfile, delimiter=',')]
  4.  
  5. def main(userPosFile):
  6.     score = 0
  7.     playing = True
  8.     while playing:
  9.         song = random.choice(songs)
  10.         print("\nThe first letter of each word in the song is",*[i[0] for i in song[0].split()],"\nThe artist is",song[1])
  11.  
  12.         for i in range(2):
  13.             if input("\nEnter your guess of what the song might be: ").upper() == song[0].upper():
  14.                 if i == 0:
  15.                     print("Correct, you get 3 points.")
  16.                     score += 3
  17.                 else:
  18.                     print("Correct, you get 1 point.")
  19.                     score += 1
  20.                 break
  21.             else:
  22.                 print("Inccorect guess.")
  23.         else:
  24.             print("\nYou ran out of guesses and lose.\nYou scored ",score)
  25.             finish(score,userPosFile)
  26.             playing = False
  27.  
  28.     leader()  
  29.  
  30. def finish(score, pos):
  31.     if int(scores[pos][2]) < score:
  32.         scores[pos][2] = score
  33.         with open("Scores.csv","w",newline='') as csvfile:
  34.             csv.writer(csvfile, delimiter=",").writerows(scores)
  35.     else:
  36.         print("You have not beaten your previous high score of",scores[pos][2])
  37.  
  38. def login():
  39.     global scores
  40.     with open("Scores.csv","r",newline='') as csvfile:
  41.         scores = [i for i in csv.reader(csvfile, delimiter=',')]
  42.     name = input("Enter/Create Username: ")
  43.     for i in range(len(scores)):
  44.         if scores[i][0] == name:
  45.             if input("Enter password: ") == scores[i][1]:
  46.                 print("Correct. Logging in...")
  47.                 main(i)
  48.                 break
  49.             else:
  50.                 print("Incorrect details. Try again.\n")
  51.                 login()
  52.                 break
  53.     else:
  54.         if input("User not found would you like to create an account? (y/n) ") == "y":
  55.             with open("Scores.csv","a",newline='') as csvfile:
  56.                 csv.writer(csvfile, delimiter = ",").writerow([name,input("Create password: "),0])
  57.             print("Login in with your new account.\n")
  58.             login()
  59.        
  60. def leader():
  61.     l = sorted(scores, key = lambda x:int(x[2]), reverse = True)
  62.     if len(l) <5:
  63.         length = len(l)
  64.     else:
  65.         length = 5
  66.     print("\nThe leaderboard is as follows: ")
  67.     [print("- Position",i+1,l[i][0],"with a score of",l[i][2]) for i in range(length)]
  68.        
  69.  
  70.  
  71. login()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement