Advertisement
Guest User

Untitled

a guest
Oct 26th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. import csv
  2. import random
  3. import os
  4.  
  5. #--CONSTANTS & FUNCS
  6. USERS = 'users.csv'
  7. SONGS = 'songs.csv'
  8.  
  9. def get_song_string(song):
  10.     '''Returns a sting based on the song argument: only each letter is shown'''
  11.     output = ''
  12.     words = song.split(' ')
  13.     for word in words:
  14.         output += word[0] + ('_'*(len(word)-1)) + ' '
  15.     return output
  16.  
  17. #--AUTHENTICATION--
  18. while True:
  19.     try:
  20.         account = {'y':True, 'yes':True, 'n':False,'no':False}[input('Do you have an account already? (y/n) ').lower()]
  21.         break
  22.     except:
  23.         pass
  24.  
  25. username = input('Username: ')
  26. password = input('Password: ')
  27.  
  28. if account:
  29.     #check & continue
  30.     found = False
  31.     with open(USERS) as f:
  32.         for row in csv.reader(f):
  33.             if row[0] == username:
  34.                 #user exists
  35.                 found = True
  36.                 if row[1] != password:
  37.                     print("You have entered an incorrect password. Please try again.")
  38.                     raise SystemExit
  39.  
  40.     if not found:
  41.         print("The account '{}' doesn't exist".format(username))
  42.         raise SystemExit
  43.  
  44. else:
  45.     #add & continue
  46.     existing = False
  47.     with open(USERS) as f:
  48.         for row in csv.reader(f):
  49.             if row[0] == username:
  50.                 existing = True
  51.  
  52.     if not existing:
  53.         with open(USERS, 'a', newline='\n') as f:
  54.             csv.writer(f).writerow([username, password])
  55.             print('Account made successfully!')
  56.     else:
  57.         print('This username is already taken, please try again with another username.')
  58.         raise SystemExit
  59.  
  60. #--GAME--
  61. with open(SONGS) as f:
  62.     songs = [row for row in csv.reader(f)]
  63.  
  64. game = True
  65. points = 0
  66. while game: #game repeats
  67.     print() #seperates authentication and each question
  68.     choice = random.choice(songs) #changes song
  69.     attempts = 2 #attempts left at each question
  70.     guess_number = 0 #keeps track of which guess the user is on
  71.  
  72.     while True: #while still playing
  73.         if attempts == 0:
  74.             game = False
  75.             break
  76.  
  77.         print('Artist: {}\nSong: {}'.format(choice[0], get_song_string(choice[1])))
  78.  
  79.         guess = input('What do you think the song is called? ')
  80.         if guess != '':
  81.             if guess.lower() == choice[1].lower():
  82.                 print('Correct!')
  83.                 if guess_number == 0: #if correct on the first go
  84.                     points += 3
  85.                 else:
  86.                     points += 1
  87.                 break
  88.             else:
  89.                 print('Incorrect!')
  90.                 attempts -= 1
  91.                 guess_number += 1
  92.  
  93. print('\nYou got {} point{}.'.format('no' if points == 0 else points, '' if points == 1 else 's'))
  94.  
  95. leaderboard = []
  96.  
  97. #write to a new file
  98. with open(USERS) as f:
  99.     reader = csv.reader(f)
  100.     with open('new_users.csv','w', newline='\n') as new_f:
  101.         writer = csv.writer(new_f)
  102.         for row in reader:
  103.             if row[0] == username and points > int(row[2]):
  104.                 new_row = [row[0],row[1],str(points)]
  105.                 leaderboard.append(new_row)
  106.                 writer.writerow(new_row)
  107.             else:
  108.                 leaderboard.append(row)
  109.                 writer.writerow(row)
  110.  
  111. #replace it with old file
  112. os.remove('users.csv')
  113. os.rename('new_users.csv', 'users.csv')
  114.  
  115. ##--LEADERBOARD--
  116. print('\n--LEADERBOARD--')
  117. leaderboard.sort(key=lambda x: -int(x[2]))
  118. for user in leaderboard[:5]:
  119.     print('{}: {}'.format(user[0], user[2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement