Jan-27-2020, 08:57 PM
I need for it to print the top 5 scores, but at the moment it is only printing the highest score of each user. Any help would be much appreciated. Here is the whole code, and then the external files that are relevant. I only have python at school and am not sure of the version, I will edit this post when I find out what python version I am using. I couldn't find out how to do it online and my teacher won't help me. I just can't think of a way to fix it.
Passwords:
Scores external file is currently empty but it would have the top score for each user inside.
Thank you very much.
import random def readData(): with open("Song Names.txt", "r") as f: songNames = [line.rstrip('\n') for line in f] f.close() with open("Artist Names.txt", "r") as f: artistNames = [line.rstrip('\n') for line in f] f.close() with open("Usernames.txt", "r") as f: usernames = [line.rstrip('\n') for line in f] f.close() with open("Passwords.txt", "r") as f: passwords = [line.rstrip('\n') for line in f] f.close() return songNames, artistNames, usernames, passwords def login(usernames, passwords): global enterUsername enterUsername = input("Enter username\n> ") if enterUsername in usernames: enterPassword = str(input("Enter password\n> ")) usernamePos = usernames.index(enterUsername) if enterPassword in passwords: passwordPos = passwords.index(enterPassword) if usernamePos == passwordPos: print ("You are logged in", enterUsername) return True else: print("You entered the wrong password...") else: print("That password is not in the file...") else: print("You're not allowed to play the game...") return False def chooseSong(songNames, artistNames): unknownSong = random.choice(songNames) sSP = songNames.index(unknownSong) unknownArtist = artistNames[sSP] return unknownSong, unknownArtist def preGame(unknownSong, unknownArtist): word_guessed = [] songs = unknownSong.split() letters = [word[0] for word in songs] print ("The artist is ", unknownArtist, "and the song is", "".join(letters)) for letter in unknownSong: if letter == " ": word_guessed.append(" / ") else: word_guessed.append("-") for letter in word_guessed: print (letter, end = " ") return unknownSong def playGame(unknownSong, score): tries = 0 while True: userGuess = str(input("\nWhat is the song name?\n> ")) if userGuess == unknownSong: print("Correct!") if tries == 0: score = score + 3 print("You earnt 3 points! You have", score, "points in total.") return score, True elif tries == 1: score = score + 1 print("You earnt 1 point! You have", score, "points in total.") return score, True else: tries = tries + 1 if tries == 2: print ("Game over! You got", score, "points!") return score, False break print("You have 1 try left.") else: print("Incorrect!") print("You have 1 try left.") def highScore(score, enterUsername): highscore = 0 last_high_score = 0 text_file = open("Scores.txt", "r") for line in text_file.readlines(): line_parts = line.split(" has a score of ") if len(line_parts) > 1: line_parts = line_parts[-1].split("\n") highscore = line_parts[0] if int(highscore) > last_high_score: last_high_score = int(highscore) if int(score) > last_high_score: text_file = open("Scores.txt", "a") text_file.write("\n"+ str(enterUsername) +' has a score of '+ str(score) +"\n") text_file.close() print ("\n") text_file = open("Scores.txt", "r") whole_thing = text_file.read() print (whole_thing) text_file.close() def main(): songNames, artistNames, usernames, passwords = readData() score = 0 success = login(usernames, passwords) if success == True: while True: print ("Rules: Song must be in full capitals, don't put a space after your answer") unknownSong, artist = chooseSong(songNames, artistNames) preGame(unknownSong, artist) score, win = playGame(unknownSong, score) if win == False: highScore(score, enterUsername) break main()Usernames:
- John
Greg
Secure
INeed
MyHighscore
Thank
Passwords:
- Smith
Heffley
Password
Help
IsntWorking
You
Scores external file is currently empty but it would have the top score for each user inside.
Thank you very much.