runnerpaul
runnerpaul

Reputation: 7226

Python program saying file is closed but it should be open

I've this Python program:

#! python3
# random_quiz_generator.py - Creates quizzes with questions and answers in random order, along with the answer key.

import random

# The quiz data. Keys are states and values are their capitals
capitals = {'Alabama': 'Montgomery',
            .......
            'Wyoming': 'Cheyenne'
            }

# Generate 35 quiz files
for quiz_num in range(35):
    # create the quiz and answer key files
    quiz_file = open(f'capitalsquiz{quiz_num + 1}.txt', 'w')
    answer_key_file = open(f'capitalsquiz_answers{quiz_num +1}.txt', 'w')

    # write out the header for the quiz
    quiz_file.write('Name:\n\nDate:\n\nPeriod:\n\n')
    quiz_file.write((' ' * 20) + f'State Capitals Quiz (Form{quiz_num +1})')
    quiz_file.write('\n\n')

    # shuffle the order of the states
    states = list(capitals.keys())
    random.shuffle(states)

    # loop through all 50 states, making a question for each
    for question_num in range(50):
        # get right and wrong answers
        correct_answer = capitals[states[quiz_num]]
        wrong_answers = list(capitals.values())
        del wrong_answers[wrong_answers.index(correct_answer)]
        wrong_answers = random.sample(wrong_answers, 3)
        answer_options = wrong_answers + [correct_answer]
        random.shuffle(answer_options)

        # write the question and answer options to the quiz file
        quiz_file.write(f'{question_num + 1}. What is the capital of {states[question_num]}?\n')
        for i in range(4):
            quiz_file.write(f"  {'ABCD'[i]}. {answer_options[i]}\n")
            quiz_file.write('\n')

        # write the answer key to a file
        answer_key_file.write(f"{question_num + 1}. {'ABCD'[answer_options.index(correct_answer)]}")
        quiz_file.close()
        answer_key_file.close()

I get this error:

Traceback (most recent call last):
  File "/RandomQuizGenerator/random_quiz_generator.py", line 85, in <module>
    quiz_file.write(f'{question_num + 1}. What is the capital of {states[question_num]}?\n')
ValueError: I/O operation on closed file.

I can't understand why this file would be closed. After quiz_file = open(f'capitalsquiz{quiz_num + 1}.txt', 'w') should it not remain open until I close it?

Upvotes: 1

Views: 97

Answers (1)

Gabio
Gabio

Reputation: 9494

The problem is that you open the file before the loop and closes it for each iteration on the last line so for the second iteration of for question_num in range(50) it will be closed.

Try to move answer_key_file.close() outside the for loop or use context manager, for which you don't have to close manually the file.

For example:

for quiz_num in range(35):
    # create the quiz and answer key files
    with open(f'capitalsquiz{quiz_num + 1}.txt', 'w') as quiz_file, open(f'capitalsquiz_answers{quiz_num +1}.txt',
                                                                         'w') as answer_key_file:
        # write out the header for the quiz
        quiz_file.write('Name:\n\nDate:\n\nPeriod:\n\n')
        quiz_file.write((' ' * 20) + f'State Capitals Quiz (Form{quiz_num +1})')
        quiz_file.write('\n\n')

        # shuffle the order of the states
        states = list(capitals.keys())
        random.shuffle(states)

        # loop through all 50 states, making a question for each
        for question_num in range(50):
            # get right and wrong answers
            correct_answer = capitals[states[quiz_num]]
            wrong_answers = list(capitals.values())
            del wrong_answers[wrong_answers.index(correct_answer)]
            wrong_answers = random.sample(wrong_answers, 3)
            answer_options = wrong_answers + [correct_answer]
            random.shuffle(answer_options)

            # write the question and answer options to the quiz file
            quiz_file.write(f'{question_num + 1}. What is the capital of {states[question_num]}?\n')
            for i in range(4):
                quiz_file.write(f"  {'ABCD'[i]}. {answer_options[i]}\n")
                quiz_file.write('\n')

            # write the answer key to a file
            answer_key_file.write(f"{question_num + 1}. {'ABCD'[answer_options.index(correct_answer)]}")

Upvotes: 5

Related Questions