Maximum numbers python file reading help
Go to solution
Solved by 0x21,
2 hours ago, DerpDiamonds1 said:So with this solution can I have multiple high scores from the same username?
No, if you want to have multiple scores from the same user then this will work:
def printScores(n): with open('scores.txt', mode='r') as file: reader = csv.reader(file) # Create a list of tuples from the text file scores = [(row[0], int(row[1])) for row in reader] # Sort into a list of the top n scores. scores = sorted(scores, key=lambda score: score[1], reverse=True)[:n] # Print if len(scores) > 0: print("The top {} scores are:\n".format(n)) pos = 1 for name, score in scores: print("\t{}. {} with {}".format(pos, name, score)) pos += 1 else: print("There are no scores")
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now