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?
#11
Question 
(Mar-05-2023, 09:17 PM)deanhystad Wrote: 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?

Hmm... I'm not sure what you're getting at, sorry. We're using a .txt file.

I forgot to put the (). My apologies.

Also, I've been trying to show you the code for what we've done. And explained it (although badly I think?). Yeah, we do, but the only thing we're allowed to use (I believe) is
import random
import os.path
import json

random.seed()


We're only allowed to use those packages. I'm just not sure how we're going to do the loading and saving in the predefined list. That's the main reason why I asked here. I've showed you the code we're working with, and we're confused on how to get it to work. Wall

Any help would be appreciated again.

Thanks. And sorry if I sounded rude.
Reply
#12
txt is only an extension. You use commas to separate name and score. You are writing a Comma Separated Values (csv) formatted text file.

You can use json? If so, do some reading about how the json package works. If you need to save and restore a dictionary, json is the way to go. You don't have to do hardly any work. Just load the file to get the dictionary, or dump the dictionary to write the dictionary to a json file.
Reply
#13
(Mar-06-2023, 02:37 PM)deanhystad Wrote: txt is only an extension. You use commas to separate name and score. You are writing a Comma Separated Values (csv) formatted text file.

You can use json? If so, do some reading about how the json package works. If you need to save and restore a dictionary, json is the way to go. You don't have to do hardly any work. Just load the file to get the dictionary, or dump the dictionary to write the dictionary to a json file.

Hello,

I've looked into a lot of tutorials on json, but it isn't a json file. It's just a list of names with the scores. I've shown code on how we approached this, but it always appears at the end of the list and we don't want that. It's difficult to think of a way to try and put it in the list. I've tried adding a list object, and then putting it through the write method, it uses strings, not lists, tried json, no luck there.

It's feeling like I'm failing them, and I don't want that to happen. I'm hitting a brick wall with everything I try. Wall .

I just want to save the things to the predefined list in the .txt. Which is in a csv format (thanks for clarifying by the way Smile), and it has been bugging me so hard for days now. Huh .

Any help would be greatly appreciated, as this student has already failed once and I'm trying to stop that from happening again.

Sorry if I sounded rude by the way. I don't mean to come off as rude.

Thanks.
Reply
#14
You are not doing a good job defining your problem.

If you are allowed to use json, you would write a json file. it would look like this:
Output:
{ "Phil Housley": 6, "Brendan Shanahan": 14, "Luk Robitaille": 20, "Marty Brodeur": 30, "Wayne Gretzky": 99 }
Notice the leading and trailing brackets. This tells the the json library that the json formatted data is a dictionary. Json also knows how to read and write lists (and strings and ints and floats and ...).

Actually, the json file would not look like that shown. json would print everything one line like this:
Output:
{"Phil Housley": 6, "Brendan Shanahan": 14, "Luk Robitaille": 20, "Marty Brodeur": 30, "Wayne Gretzky": 99}
This is a text file, and you could even use a .txt extenson, but it is common practice to use .json as the extension for a json file.

A json file would be really nice for saving a leaderboard. You would load the json file which would create a dictionary of names:score items. You could add names and scores to the dictionary and dump the updated dictionary to the json file. You wouldn't have to do any manipulations at all to extract names and scores from the file. json would do all the work.

You could also save the leaderboard in a CSV file, This is just a text file that contains columnar data. The columns are separated by a separator character, usually a comma (Thus the Comma Separated Values name).

To write a CSV file, all you have to do is write the values separated by the separator character. You did this in an earlier post. To read the file you do is the reverse. You did not include the csv package in the list of packages you are allowed to use. The csv package would make it easier to read the csv formatted file, but it isn't required. You can read the file a line at a time and split it into substrings based on the separator character. For a leaderboard you would also need to convert the score substrings into numbers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to update score value? Mavoz 5 4,980 Nov-08-2022, 12:37 AM
Last Post: Mavoz
  How to correct the NameError: name 'xx' is not defined? vokoyo 5 18,006 Feb-17-2021, 05:55 AM
Last Post: delonbest
  NameError: name 'os' is not defined, & load_files(sys.argv[1]) AryaIC 3 6,383 Nov-07-2020, 07:45 PM
Last Post: jefsummers
  Error in code NameError: name ' ' is not defined ppman00 11 12,062 Sep-18-2020, 05:22 AM
Last Post: ndc85430
  NameError: name 'print_string' is not defined jamie_01 2 2,806 Jun-11-2020, 05:27 AM
Last Post: buran
  "NameError: name 'catName1' is not defined tofif 3 7,140 Jun-24-2019, 06:05 AM
Last Post: perfringo
  NameError x not defined Bruizeh 5 8,915 Feb-27-2019, 10:59 AM
Last Post: Larz60+
  NameError: name 'mailbox_list' is not defined pythonnewb 2 5,588 Aug-06-2017, 09:31 PM
Last Post: pythonnewb
  Average score MartinEvtimov 5 8,530 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