import random import os def load_vocabulary(filepath): vocab = {} try: with open(filepath, 'r') as file: for line in file: eng, fra = line.strip().split(',') vocab[eng] = fra except FileNotFoundError: print("Error: The French_English data file could not be found.") except ValueError: print("Error: File data is malformed. Ensure 'English,French' format.") return vocab def get_valid_student(): name = input("Enter student name: ") while True: try: candidate_num = int(input("Enter candidate number (0001-9999): ")) if 1 <= candidate_num <= 9999: return name, f"{candidate_num:04d}" print("Invalid range. Number must be between 0001 and 9999.") except ValueError: print("Invalid input type. Please enter a numerical value.") def main(): vocab = load_vocabulary("French_English.txt") if not vocab: return name, cand_num = get_valid_student() correct_words = set() score_history = [] while True: available = [w for w in vocab.keys() if w not in correct_words] if len(available) < 10: print("Not enough new words left for a full 10-question quiz.") break questions = random.sample(available, 10) current_score = 0 for word in questions: answer = input(f"What is the French translation for '{word}'? ") if answer.strip().lower() == vocab[word].lower(): print("Correct!") current_score += 1 correct_words.add(word) else: print(f"Incorrect. The correct spelling is '{vocab[word]}'.") score_history.append(current_score) attempts = len(score_history) print(f"\nAttempt {attempts} finished. You scored {current_score} out of 10.") try: with open("student_results.txt", "a") as results_file: results_file.write(f"{name},{cand_num},Attempt_{attempts},{current_score}/10\n") except IOError: print("Error: Could not save results to file.") if attempts >= 2: average = sum(score_history) / attempts print(f"Average score after {attempts} attempts: {average:.2f}/10") if input("\nWould you like to try another attempt? (y/n): ").lower() != 'y': break if __name__ == "__main__": main()