-1

i have an assignment coming up in which i have to code a dice game . its multiplayer random luck with gambling points and etc

one of the things its asking for though is to save the winning players name and their score into a text file and at the end print the five highest scores in the text file with the five players names, ive got the code so that it saves the players name and score along with some text but have absolutely no idea on how to read the whole text file and and pick out lines that have the 5 largest integers and print them

`

name = str(input("Player 1 name"))
name2 = str(input("Player 2 name"))
score = str(input("Player 1 score"))
score2 = str(input("Player 2 score"))
text_file = open("CH30.txt", "r+")
if score > score2:
    content = text_file.readlines(30)
    if len(content) > 0 :
        text_file.write("\n")
    text_file.write(name)
    text_file.write (" wins with ")
    text_file.write (score)
    text_file.write (" points")
else:
    content = text_file.readlines(30)
    if len(content) > 0 :
        text_file.write("\n")
    text_file.write (name2)
    text_file.write (" wins with ")
    text_file.write (score2)
    text_file.write (" points")


`

the full game is not attached as I'm at my dads house currently and forgot to bring my usb stick any help on how to do this would be much appreciated :)

2 Answers2

1

If your data in file has this format:

Player1 wins with 30 points
Player2 wins with 40 points
Player3 wins with 85 points
Player5 wins with 45 points
Player7 wins with 10 points
Player6 wins with 80 points
Player4 wins with 20 points
Player9 wins with 90 points
Player8 wins with 70 points
Player11 wins with 120 points
Player10 wins with 15 points

and there is no space in the first line of the file, because you have text_file.write("\n") written in the first line. Please add it at the end of the line, i.e the last file write.

For example:

text_file.write(name)
text_file.write (" wins with ")
text_file.write (score)
text_file.write (" points")
text_file.write("\n")

Here is how to get the 5 highest scores of the file:

text_file = open('CH30.txt.', 'r')
Lines = text_file.readlines()
PlayersScores = []

# read each line get the player name and points 
for line in Lines:
    # split the line into list of strings
    line = line.split(" ")
    # removing \n from last element
    line[-1] = line[-1].replace("\n", "")
    print(line)
    # find player name position
    playerName = line.index("wins") - 1
    # find points position
    points = line.index("points") - 1
    # add the tuple (playerName, points) in a list
    PlayersScores.append((line[playerName], line[points]))
# descending order sort by player score
PlayersScores = sorted(PlayersScores, key=lambda t: t[1], reverse=True)

# get the first 5 players
print("Highest Scores:\n")
for i in range(5):
    print(str(i+1) + ". " + PlayersScores[i][0] + " " + PlayersScores[i][1] + " points")

Output example:

Highest Scores:

1. Player11 120 points
2. Player9 90 points
3. Player3 85 points
4. Player6 80 points
5. Player8 70 points
serafm
  • 59
  • 7
0

First of all, you should read the file to a list of lines. If you don`t know how to do this, check:
How to read a file line-by-line into a list?

Next, you have to sort the lines. We can use lambda to achieve it. Assuming, that the single line looks like this:
"Rick wins with 98 points"

Sorting function would look like this:
scores.sort(key= lambda x : int(x.split(" ")[3]))

Then, you just need to slice the last five elements.

Please note, this solution is prone to many errors, as you use the file in human-readable format. What's easily readable for us, is often hard to read by computer (and vice versa). So, if you don't want to use the file as a bedtime reading, consider using some formats, that are easier to parse - i.e. CSV

Ni3dzwi3dz
  • 177
  • 8
  • Sorry i have to make the file human readable as its one of the requirements to make the file readable to anyone looking at scores, also i dont fully understand what to do after the line-by-line reading with ```scores.sort(key= lambda x : int(x.split(" ")[3])) ``` should i put it within the if statement and what should i put instead of scores.sort as im getting an error message ``` File "c:\py.files\test.py", line 24, in score.sort(key= lambda x : int(x.split(" ")[3])) AttributeError: 'str' object has no attribute 'sort'``` If youcant help thats fine and thanks anyway – Jamie Cooper Nov 26 '22 at 19:32