Posts: 15
Threads: 7
Joined: Oct 2022
Mar-03-2023, 12:48 PM
Hi,
I'm going to be blunt, I'm here, helping out another student, and I need help. The error is in the title, and here's the code:
def load_scores(score):
name = input('What is your name?: ')
with open(r'leaderboard.txt', 'a') as f:
f.write(',' + name + ' ' + str(score))
f.close()
with open(r'leaderboard.txt') as leaders:
return leaders
load_scores(score) Any help would be greatly appreciated.
Thank you very much.
Posts: 4,790
Threads: 76
Joined: Jan 2018
When you call load_scores(score) , you haven't yet given a value to the variable 'score'. That's why Python complains.
Posts: 1,358
Threads: 2
Joined: May 2019
In line 9, what is the current value of score?
That is what is undefined.
Posts: 15
Threads: 7
Joined: Oct 2022
Mar-03-2023, 08:56 PM
(This post was last modified: Mar-03-2023, 08:56 PM by MrKnd94.)
Okay. New problem now. When we input this line of code in, it outputs: "<_io.TextIOWrapper name='leaderboard.txt' mode='r' encoding='cp1252'>".
def load_scores():
with open(r'leaderboard.txt') as leaders:
return leaders
print(load_scores()) We want the program to print the words from the file, not that.
So, how can we do it?
We've also tried, and failed, to do this code, but the same error occurs.
def load_scores():
with open(r'leaderboard.txt') as leaders:
print(leaders.read())
print(load_scores)) Any help would be greatly appreciated again.
Thanks.
Edit:
We've just tried this, but still received the same thing at the end, which we don't want.
def load_scores():
with open(r'leaderboard.txt') as leaders:
print(leaders.read())
leaders.close
return leaders
print(load_scores()) And yes, we did the "return leaders" in the same indentation as well, still the same issue.
Posts: 4,790
Threads: 76
Joined: Jan 2018
Mar-03-2023, 10:25 PM
(This post was last modified: Mar-03-2023, 10:26 PM by Gribouillis.)
In your code, leaders is a file object, this is not what you want to return. The expression leaders.read() returns a string of characters read from the file. The function load_scores() must return this string to the calling statement.
Printing something (with the print function) and returning something (with the return statement) are two different things. When you print something, it goes to the user, but when you return something, it goes to the code that called the function. Most functions don't print anything but they return information to the caller.
Posts: 6,799
Threads: 20
Joined: Feb 2020
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?
Posts: 15
Threads: 7
Joined: Oct 2022
(Mar-03-2023, 10:31 PM)deanhystad Wrote: 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?
To load the names and scores, and append them in a list. I've been trying to do the saving part for days now, but no luck. The saving is in another function. The list in the "leaderboard.txt" says Quote:{"Bob": -1, "Bobby": 25, "Jim": 15, "Mary": 35}
, etc., etc.. I'll try and get the saving mechanic completed, although it should already work. And it's still failing the marking program we've been given (the load_scores() function), but I don't know why.
Posts: 6,799
Threads: 20
Joined: Feb 2020
You are writing a CSV file. You could use the CSV library or pandas to make it easier to read the file and convert into a dictionary.
Easier still is saving the data in json format. The json library will do all the conversions for you.
Posts: 15
Threads: 7
Joined: Oct 2022
(Mar-05-2023, 05:03 PM)deanhystad Wrote: You are writing a CSV file. You could use the CSV library or pandas to make it easier to read the file and convert into a dictionary.
Easier still is saving the data in json format. The json library will do all the conversions for you.
Hello,
They've imported the json package(?) in the Python file. We have to use .txt, as it is in the assignment. This is the code for the saving. score = 999
def save_score(score):
name = input("What is your name?: ")
with open(r'leaderboard.txt', 'a') as f:
f.write(', ' + name + ' ' + str(score))
f.close
return
save_score(score)
print("Made changes") But it won't but it in the list in the .txt file. And we're using 'a' to append. Why won't it work?
Any help would be appreciated.
Thanks.
Posts: 6,799
Threads: 20
Joined: Feb 2020
A json file is text, just like a CSV file is text. Both are just a special way to format text in a file. Probably a good idea to stick with a CSV format file as it looks more "texty".
BTW, this does nothing:
f.close If you want to close the file it should be f.close(). However, you don't need to close the file because that is handled by your file open context manager (with open(r'leaderboard.txt', 'a') as f:).
You need to tell us what problem you are having, and you should post some code showing how you are trying to read the information from the file. So far, the only code you've posted is one that reads strings from the file. You need something that will read names and scores. Since it is a CSV format file I wonder if there are any Python tools for reading that kind of file?
|