ValueError:关闭文件的I/O操作为什么会发生这种情况?使用python书自动处理无聊的事情



Am beginer遵循这本书使用python自动处理无聊的东西

本章的第一个项目显示如何使用Random制作随机测验生成器,编写、阅读、关闭和打开

这是我的代码

#! python3
# randomQuizGenerator.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', 'Alaska': 'Juneau', 'Arizona': 'Phoenix',
'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver',
'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee',
'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', 'Illinois':
'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas':
'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', 'Maine':
'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston', 'Michigan':
'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson', 'Missouri':
'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada':
'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 
'NewMexico': 'Santa Fe', 'New York': 'Albany',
'North Carolina': 'Raleigh', 'North Dakota': 'Bismarck', 
'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City',
'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence',
'South Carolina': 'Columbia', 'South Dakota': 'Pierre', 'Tennessee':
'Nashville', 'Texas': 'Austin', 'Utah': 'Salt Lake City', 'Vermont':
'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 
'WestVirginia': 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}
# Generate 35 quiz files.
for quizNum in range(35):
# Create the quiz and answer key files.
quizFile = open(f'capitalsquiz{quizNum + 1}.txt', 'w')
answerKeyFile = open(f'capitalsquiz_answers{quizNum + 1}.txt', 'w')
# Write out the header for the quiz.
quizFile.write('Name:nnDate:nnPeriod:nn')
quizFile.write((' ' * 20) + f'State Capitals Quiz (Form{quizNum + 1})')
quizFile.write('nn')
# Shuffle the order of the states.
states = list(capitals.keys())
random.shuffle(states)
# Loop through all 50 states, making a question for each.
for questionNum in range(50):
# Get right and wrong answers.
correctAnswer = capitals[states[questionNum]]
wrongAnswers = list(capitals.values())
del wrongAnswers[wrongAnswers.index(correctAnswer)]
wrongAnswers = random.sample(wrongAnswers, 3)
answerOptions = wrongAnswers + [correctAnswer]
random.shuffle(answerOptions)
# Write the question and the answer options to the quiz file.
quizFile.write(f'{questionNum + 1}. What is the capital of{states[questionNum]}?n')
for i in range(4):
quizFile.write(f"    {'ABCD'[i]}. { answerOptions[i]}n")
quizFile.write('n')
# Write the answer key to a file.
answerKeyFile.write(f"{questionNum + 1}.{'ABCD'[answerOptions.index(correctAnswer)]}")
quizFile.close()
answerKeyFile.close()

以下是程序的作用:

  1. 创建35个不同的测验

  2. 按随机顺序为每个测验创建50道选择题以随机顺序为每个问题提供正确答案和三个随机错误答案

  3. 将测验写入35个文本文件

  4. 将答案键写入35个文本文件

这意味着代码需要执行以下操作:

  1. 将状态及其大写字母存储在字典中
  2. 为问答键文本文件调用open((、write((和close((
  3. 使用random.shuffle((将问题和多项选择题的顺序随机化

但是此代码给出一个错误

ValueError:对已关闭文件的I/O操作

追溯(最近一次调用最后一次(:文件"/home/hp/Documents/Development/pob/ch9/randomQuizGenarotor.py";,第50行,inquizFile.write(f'{questionNum+1}。{states[questionNum]}的大写字母是多少?\n'(ValueError:对关闭的文件执行I/O操作

当我删除代码时,它可以工作

quizFile.close()
answerKeyFile.close()

但也存在一些问题

我想将就一下

看起来close()函数在for循环中,因此在第一次迭代后,它将关闭文件。将close()功能移到的外部

最新更新