我正在用 JSON 为 Python 聊天机器人编写一些数据,这是我的 JSON 结构/格式:
"question": "add question here",
"answers": [
"answers here"
]
"correctAnswer": 0
那么有谁知道如何选择任何一个答案,例如问候语?预先感谢!
random.choice
随机选择任何答案。{
"question": "add question here",
"answers": [
"answers here",
"another answer here",
"third answer here"
],
"correctAnswer": 0
}
import random
import json
with open("data.json", "r") as f:
data = json.load(f)
print(random.choice(data["answers"]))