Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import random
- import os
- #--CONSTANTS & FUNCS
- USERS = 'users.csv'
- SONGS = 'songs.csv'
- def get_song_string(song):
- '''Returns a sting based on the song argument: only each letter is shown'''
- output = ''
- words = song.split(' ')
- for word in words:
- output += word[0] + ('_'*(len(word)-1)) + ' '
- return output
- #--AUTHENTICATION--
- while True:
- try:
- account = {'y':True, 'yes':True, 'n':False,'no':False}[input('Do you have an account already? (y/n) ').lower()]
- break
- except:
- pass
- username = input('Username: ')
- password = input('Password: ')
- if account:
- #check & continue
- found = False
- with open(USERS) as f:
- for row in csv.reader(f):
- if row[0] == username:
- #user exists
- found = True
- if row[1] != password:
- print("You have entered an incorrect password. Please try again.")
- raise SystemExit
- if not found:
- print("The account '{}' doesn't exist".format(username))
- raise SystemExit
- else:
- #add & continue
- existing = False
- with open(USERS) as f:
- for row in csv.reader(f):
- if row[0] == username:
- existing = True
- if not existing:
- with open(USERS, 'a', newline='\n') as f:
- csv.writer(f).writerow([username, password])
- print('Account made successfully!')
- else:
- print('This username is already taken, please try again with another username.')
- raise SystemExit
- #--GAME--
- with open(SONGS) as f:
- songs = [row for row in csv.reader(f)]
- game = True
- points = 0
- while game: #game repeats
- print() #seperates authentication and each question
- choice = random.choice(songs) #changes song
- attempts = 2 #attempts left at each question
- guess_number = 0 #keeps track of which guess the user is on
- while True: #while still playing
- if attempts == 0:
- game = False
- break
- print('Artist: {}\nSong: {}'.format(choice[0], get_song_string(choice[1])))
- guess = input('What do you think the song is called? ')
- if guess != '':
- if guess.lower() == choice[1].lower():
- print('Correct!')
- if guess_number == 0: #if correct on the first go
- points += 3
- else:
- points += 1
- break
- else:
- print('Incorrect!')
- attempts -= 1
- guess_number += 1
- print('\nYou got {} point{}.'.format('no' if points == 0 else points, '' if points == 1 else 's'))
- leaderboard = []
- #write to a new file
- with open(USERS) as f:
- reader = csv.reader(f)
- with open('new_users.csv','w', newline='\n') as new_f:
- writer = csv.writer(new_f)
- for row in reader:
- if row[0] == username and points > int(row[2]):
- new_row = [row[0],row[1],str(points)]
- leaderboard.append(new_row)
- writer.writerow(new_row)
- else:
- leaderboard.append(row)
- writer.writerow(row)
- #replace it with old file
- os.remove('users.csv')
- os.rename('new_users.csv', 'users.csv')
- ##--LEADERBOARD--
- print('\n--LEADERBOARD--')
- leaderboard.sort(key=lambda x: -int(x[2]))
- for user in leaderboard[:5]:
- print('{}: {}'.format(user[0], user[2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement