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?

4
  • 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. Commented Dec 3, 2022 at 4: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 Commented Dec 3, 2022 at 4:38
  • Generally, load the whole JSON file (if present), modify data and write (not append) it to the file as a whole. Commented Dec 3, 2022 at 4: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. Commented Dec 3, 2022 at 7:53

1 Answer 1

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 :)

Sign up to request clarification or add additional context in comments.

10 Comments

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?
hi there. you should put a dictionary in your json file first. even an empty one. stackoverflow.com/questions/8930915/…
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:
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 <module> f.dump(dct,f) ^^^^^^ AttributeError: '_io.TextIOWrapper' object has no attribute 'dump'
turn it to json.dump(dct,f) like my code above
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.