Spaces:
Running
Running
File size: 1,754 Bytes
5798cfc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import json
import os
def read_questions_from_json(file_path):
"""
Reads questions from a JSON file.
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file '{file_path}' does not exist.")
with open(file_path, 'r') as f:
questions = json.load(f)
if not questions:
raise ValueError("The JSON file is empty or has invalid content.")
return questions
def conduct_interview(questions):
"""
Conducts an interview by printing each question, taking input for the answer,
and storing the questions and answers in a list.
"""
interview_data = []
print("\n--- Interview Started ---\n")
for question in questions:
print(f"{question}")
answer = input("Your answer: ").strip()
interview_data.append({"question": question, "answer": answer})
print("\n--- Interview Completed ---\n")
return interview_data
def save_interview_to_file(interview_data, file_path):
"""
Saves the questions and answers to a text file.
"""
with open(file_path, 'w') as f:
for entry in interview_data:
f.write(f"Q: {entry['question']}\n")
f.write(f"A: {entry['answer']}\n\n")
print(f"Interview saved to {file_path}")
if __name__ == "__main__":
QUESTIONS_FILE_PATH = "questions.json"
INTERVIEW_FILE_PATH = "interview.txt"
try:
# Read questions from JSON file
questions = read_questions_from_json(QUESTIONS_FILE_PATH)
# Conduct the interview
interview_data = conduct_interview(questions)
# Save the interview to a text file
save_interview_to_file(interview_data, INTERVIEW_FILE_PATH)
except Exception as e:
print(f"Error: {e}")
|