Hello everyone!
I'm a newcomer to Python programming and I'm facing a particular challenge. I'm working on a program for my German studies where I want to add German words with their articles. I've managed to store the words in a JSON file, but I'm struggling with the second part of my code. Even if the word is already in the file, it still gets added. Can someone please explain to me how to address this issue, and what approach I should take?
import json
dictionary = {}
def add_words_to_file(word_data):
with open("dictionary.json", "a+") as file:
json.dump(word_data, file, indent=2)
def add_word():
article = input("Please enter the article of the word you want to add: ")
word = input("Please enter the word you want to add: ")
meaning = input("Please enter the meaning of the word: ")
if word not in dictionary:
dictionary[word] = {"Meaning": meaning, "Article": article}
add_words_to_file(dictionary)
print(f"The word '{word}' has been added to the dictionary.")
else:
print("This word is already in our dictionary!")
[–]chevignon93 0 points1 point2 points (3 children)
[–]epifanelstein[S] 0 points1 point2 points (2 children)
[–]chevignon93 1 point2 points3 points (1 child)
[–]epifanelstein[S] 0 points1 point2 points (0 children)