talkpython / python-for-absolute-beginners-course

Code samples and other handouts for our course.
https://training.talkpython.fm/courses/explore_beginners/python-for-absolute-beginners
MIT License
2.3k stars 1k forks source link

Chapter 9 Rock Paper Scissors #5

Closed Anduie closed 4 years ago

Anduie commented 4 years ago

I have been having issues following along with getting errors that seemed inconsistent with what I thought I should be getting. So, I copied your source for Chapter 9 (linky) and I got the following errors:

Traceback (most recent call last): File "(you don't need to see my roots)/RPS1.py", line 181, in main() File "(you don't need to see my roots)/RPS1.py", line 14, in main show_leaderboard() File "(you don't need to see my roots)/RPS1.py", line 31, in show_leaderboard leaders = load_leaders() File "(you don't need to see my roots)/RPS1.py", line 152, in load_leaders return json.load(fin) File "(you don't need to see my roots)\Programs\Python\Python38-32\lib\json__init.py", line 293, in load return loads(fp.read(), File "(you don't need to see my roots)Programs\Python\Python38-32\lib\json\init__.py", line 357, in loads return _default_decoder.decode(s) File "(you don't need to see my roots)\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "(you don't need to see my roots)\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process finished with exit code 1

I'm getting the same errors when I'm working on my own sheet along with you and I'm on the Demo: Saving Wins lecture.

So, if I'm reading this correctly, the JSON library is not consistent with what you are wanting it to do. How would I update this library?

mikeckennedy commented 4 years ago

Hi, it looks like something is wrong with your file. Possibly not loading with r so overwriting it or something. But try changing this line:

return json.load(fin)

with this:

text = fin.read()
print("The text is:")
print(text)
return json.loads(text)

To see that you have a valid json file (starting with { and so on).

Anduie commented 4 years ago

That works. I think what happened was I ran part of the code without creating a null dictionary first. So, the JSON was existing as an empty file.

mikeckennedy commented 4 years ago

Glad to hear that fixed it!