0

I am doing my first Python program and its Hangman game. I managed to make it work but as a part of the task I need to write "best results -hall of fame" table as json file. Each entry in the table should consist of name of the person and the result they achieved (number of tries before guessing a word). My idea is to use dictionary for that purpose and to append the result of each game to that same dictionary.

My code goes like this:

with open("hall.json","a") as django:
    json.dump(hall_of_fame, django)

hall_of_fame is a dictionary where after playing a game the result is saved in the form of {john:5}

The problem I have is that after playing several games my .json file looks like this:

{john:5}{ana:7}{mary:3}{jim:1}{willie:6}

instead I want to get .json file to look like this:

{john:5,ana:7,mary:3,jim:1,willie:6}

What am I doing wrong? Can someone please take a look?

Mark
  • 90,562
  • 7
  • 108
  • 148
Rosohatica
  • 13
  • 2
  • 1
    It's difficult to imagine how you could end up with either of those json files since they are not valid json. I will also be difficult to help with the problem if you don't show any code other than the code you are using to save the data. – Mark Dec 03 '22 at 04:36
  • @Mark is it because the file is being opened in "a" mode (append) which would generate a file like that. which appends the item not changing the total structure of the previous item – Andrew Ryan Dec 03 '22 at 04:38
  • Generally, load the whole JSON file (if present), modify data and write (not append) it to the file as a whole. – Michael Butscher Dec 03 '22 at 04:41
  • Thank you guys. Will try out everything said. Mark, I didn˛t post the whole code because I think it would be confusing since input menus are in different language. If I don`t figure this out, I will translate all and post it here. – Rosohatica Dec 03 '22 at 07:53

1 Answers1

1

you should read your old json content. then append new item to it. an finally write it to your json file again. use code below:

with open ("hall.json") as f:
    dct=json.load(f)

#add new item to dct
dct.update(hall_of_fame)

#write new dct to json file
with open("hall.json","w") as f:
    json.dump(dct,f)

have fun :)

nariman zaeim
  • 412
  • 2
  • 6
  • 14
  • Thanks nariman. I tried what you suggested but it looks like ".update" is not recognized as a function. Do I need to load some library? – Rosohatica Dec 03 '22 at 04:58
  • hi there. you should put a dictionary in your json file first. even an empty one. https://stackoverflow.com/questions/8930915/append-a-dictionary-to-a-dictionary – nariman zaeim Dec 03 '22 at 05:05
  • Still doesnt work. .update has white letters in Visual studio. When I hoover mouse over the .update it says "update : Any" and when running the code it says: – Rosohatica Dec 03 '22 at 05:16
  • This is what it gives when running the code Traceback (most recent call last): File "c:\Users\roso\Desktop\Python\hangman.py", line 82, in f.dump(dct,f) ^^^^^^ AttributeError: '_io.TextIOWrapper' object has no attribute 'dump' – Rosohatica Dec 03 '22 at 05:18
  • turn it to json.dump(dct,f) like my code above – nariman zaeim Dec 03 '22 at 05:21
  • Nariman, I get this errors – Rosohatica Dec 03 '22 at 07:50
  • Traceback (most recent call last): File "c:\Users\dkolaric\Desktop\Python\ File "c:\Users\dkolaric\Desktop\Python\hangman.py", line 77, in dct=json.load(f) ^^^^^^^^^^^^ File "C:\Users\Rosohatica\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 293, in load return loads(fp.read(), ^^^^^^^^^^^^^^^^ File "C:\Users\Rosohatica\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) – Rosohatica Dec 03 '22 at 07:51
  • File "C:\Users\ Rosohatica \AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ Rosohatica \AppData\Local\Programs\Python\Python311\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) – Rosohatica Dec 03 '22 at 07:51
  • @Rosohatica befor you run your code you should create hall.json file and inside it you just put { } as an empty dictionary. – nariman zaeim Dec 03 '22 at 08:01
  • nariman thanks, your code worked at the end :) I redid everything and I must have been doing something wrong before. – Rosohatica Dec 03 '22 at 15:52