Python Forum
Highscore problem, need to print top 5
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Highscore problem, need to print top 5
#1
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.

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:

Scores external file is currently empty but it would have the top score for each user inside.
Thank you very much.
Reply
#2
Context: Sorry I should have explained what the assignment is, basically I had to make a music game where you need to log in to the game, and then you would be given a music artist and the first letter of each word in one of their songs, this bit is random. Then the user would input their guess of what the song is, if guessed correctly on their first go then they would earn 3 points, on their second go, 1 point and third go, you lose the game. The user has to try and get as many points as possible. There is a high score system which is all recorded in an external file. The game will output the top 5 highscores. I will be adding a system where you can register a new log in, which I can do myself but as I have stated before, my problem is getting it print the top 5 scores rather than the top score of each user. Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with print formatting using definitions Tberr86 2 2,012 Mar-20-2021, 06:23 PM
Last Post: DPaul
  List of Objects print <__main. Problem Kol789 10 3,540 Jul-21-2020, 09:37 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020