0

I am trying to display scores from positions 1 to 10 (most wins). For example:

  1. Test1 - 5 wins
  2. Test2 - 4 wins
  3. Test3 - 2 wins
  4. N/A
  5. N/A

etc.

I am using a notepad file which stores usernames, scores and passwords (in order), here's the inside look of it (users.txt):

Test1,5,TestPassword$!14

Yes,63,TestPassword$!3

Ok,35,TestPassword$!152

From here on, I figured out how to replace amount of wins with new but now I have to figure out how to display positions of winners and I've been having a difficult time. I could either try to somehow have scores (split by "," and be known as split[1]) be compared then printed out or order the lines in order of most wins from within the notepad.

Things I tried (mostly from other posts to see as a basic format of which would work best):

position = 0
choice = input("Want to see the leaderboard (Y/N)?")
if choice == "Y":
   file = open("users.txt","r")
    lines = file.readlines()
    for each in lines:
        position = position + 1
        split = (each.split(","))
        print(str(position)+".\n"+"Username:",split[0]+"\nWins:",split[1])
#not for sorting just printing out in order
num_list = []
file = open("users.txt","r")
lines = file.readlines()
for each in lines:
    num_list.append(int((each.split(","))[1]))
print(num_list)
print("Max number is -" ,max(num_list))
print("Line number is - ", (num_list.index(max(num_list)))+1)
num_list = []
file = open("users.txt","r")
lines = file.readlines()
for each in lines:
    num_list.append(int((each.split(","))[1]))
print(num_list)
max1 = num_list[0]
max2 = num_list[1]
max3 = num_list[2]
for num in num_list:
    if num > max1:
        max2 = max1
        max1 = num
    elif num > max2:
        max2 = num
print(max1)
print(max2)
nums = [1,151,2,53215,31,593,9192,5128,3818,5,38,3]
new_list = set(nums)
for i in range(10):
    high = max(new_list)
    new_list.remove(high)
    print(max(new_list))

TL;DR display positions from most wins in order of 1 to 10 from notepad file.

2
  • sorted may help you Commented Mar 19, 2022 at 20:46
  • I'll check it out :) Commented Mar 19, 2022 at 22:40

1 Answer 1

1

Firstly I would suggest using csv files which are generally more suited for your type of data.

There's a few ways to tackle your problem, the easiest for me is to use pandas:

import pandas as pd
# read the data
raw_data = pd.read_csv("users.txt")
# assign column headers for convenience
raw_data.columns = ["user_name", "score", "password"]
# sort by score
sort_by_score = raw_data.sort_values("score",ascending=False)
print(sort_by_score)

If you want to use the open function, then I would suggest using ordered dictionaries

Sign up to request clarification or add additional context in comments.

1 Comment

I've been asked to not use modules, but thank you still

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.