Answer the question
In order to leave comments, you need to log in
How to randomly print values by comparing names in a json file in python?
Hello.
I have this json file:
import json
from random import choice
with open('keywords.json', encoding='utf-8') as json_file:
data = json.load(json_file)
for item in data["dictionary"]:
for item2 in item:
for item3 in item[item2]:
print(choice(item[item2]))
Answer the question
In order to leave comments, you need to log in
Well, then you should make the dictionary values not a list, but a dictionary, in order to better navigate.
{
"dictionary": {
"как тебя зовут?":[
"Мои тестовые данные",
"Тестовые данные"
],
"как ты поживаешь?":[
"Тестовые данные",
"Всё хорошо, надеюсь у тебя тоже всё хорошо :)"
],
"привет":[
"Привет, как тебя зовут?",
"Здравствуй! Поговорим?"
]
}
}
from random import choice
# JSON в переменной data
message = 'Привет' # текст, который ввел пользователь
if message.lower() in data['dictionary']:
print(choice(data['dictionary'][message.lower()]))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question