Python Forum
NameError: name 'score' is not defined - Help?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'score' is not defined - Help?
#1
Exclamation 
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.
Reply
#2
When you call load_scores(score), you haven't yet given a value to the variable 'score'. That's why Python complains.
Reply
#3
In line 9, what is the current value of score?
That is what is undefined.
Reply
#4
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.
Reply
#5
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.
Reply
#6
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?
Reply
#7
(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.
Reply
#8
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.
Reply
#9
(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.
Reply
#10
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to update score value? Mavoz 5 2,614 Nov-08-2022, 12:37 AM
Last Post: Mavoz
  How to correct the NameError: name 'xx' is not defined? vokoyo 5 11,471 Feb-17-2021, 05:55 AM
Last Post: delonbest
  NameError: name 'os' is not defined, & load_files(sys.argv[1]) AryaIC 3 4,817 Nov-07-2020, 07:45 PM
Last Post: jefsummers
  Error in code NameError: name ' ' is not defined ppman00 11 6,509 Sep-18-2020, 05:22 AM
Last Post: ndc85430
  NameError: name 'print_string' is not defined jamie_01 2 2,106 Jun-11-2020, 05:27 AM
Last Post: buran
  "NameError: name 'catName1' is not defined tofif 3 5,746 Jun-24-2019, 06:05 AM
Last Post: perfringo
  NameError x not defined Bruizeh 5 5,402 Feb-27-2019, 10:59 AM
Last Post: Larz60+
  NameError: name 'mailbox_list' is not defined pythonnewb 2 4,814 Aug-06-2017, 09:31 PM
Last Post: pythonnewb
  Average score MartinEvtimov 5 6,966 Apr-02-2017, 07:35 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020