0

Doesn't matter if the spaces are in the file i just cant have them show.

bigscore = str(bigscore)
leaderboard = []
highscore = open("highscore.txt", mode = "r+")
for each in highscore:
    leaderboard.append(each.strip())
leaderboard.append(winner)
leaderboard.append(bigscore)
scores = []
for each in range(1, 12):
    if each % 2 != 0:
        x = int(leaderboard[each])
        scores.append(x)

scores.sort()
newleaderboard = []
loser = str(scores[0]).strip()
losernum = leaderboard.index(loser)
leaderboard.pop(losernum)
leaderboard.pop(losernum - 1)
scores.pop(0)
for each in scores:
    each = str(each).strip()
    sort = leaderboard.index(each)
    newleaderboard.insert(0, leaderboard[sort].strip())
    newleaderboard.insert(0, leaderboard[sort - 1].strip())
open("highscore.txt", mode = "w")
for each in (newleaderboard):
    highscore.write(each.replace(" ", ""))
    highscore.write("\n")
print("Here is the new leaderBoard")
for each in newleaderboard:
    each.rstrip()
    each.strip()
    each.lstrip()
    each.replace("\x00","")
    each.replace(" ","")
    print(each.strip())

my code sorts the name and score of players in an external file.
It keeps the name together with the person that gets it and saves to an external file.
However, it adds spaces when i write to the file.

But i do not want to change the way i did it (eg. using a dictionary instead)
It is meant to be my work but i need help on the spaces and the spaces only.

I cant fix it and my teacher cant.

Disclaimer-this is for the new NEA, help from forums online are now allowed, and all credit will be given when i submit it, this was a last resort.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
oscar
  • 9
  • 1
  • `each.strip()` already does left and right strip, but it *returns a new value*. Same for replace because *strings are immutable* – OneCricketeer Sep 27 '18 at 14:28
  • It might help to add what the inputs look like in the original file, what the outputs look like, and what you _want_ the outputs to look like – G. Anderson Sep 27 '18 at 14:45
  • Possible duplicate of [Python .strip method not working](https://stackoverflow.com/questions/40444787/python-strip-method-not-working) – stovfl Sep 27 '18 at 15:16

0 Answers0