Mar-03-2023, 10:31 PM
This function returns file-like object that you can use to read a file. It does not return the contents of a file.
def load_scores(): with open(r'leaderboard.txt') as leaders: return leaders print(load_scores())This function doesn't return a value at all, so printing will print None (a blank).
def load_scores(): with open(r'leaderboard.txt') as leaders: return leaders print(load_scores())This function is the same as above except you added a print. Same mistake with more output.
def load_scores(): with open(r'leaderboard.txt') as leaders: print(leaders.read()) leaders.close return leaders print(load_scores())This last example produces some output that looks like what you want. What happens if you return leaders.read()?
def load_scores(): with open(r'leaderboard.txt') as leaders: return(leaders.read()) print(load_scores())This time the function returns a str object that you can print and it will look like a leaderboard. But it isn't a leaderboard. It knows nothing about scores or names. What is the desired result in this project?