Reputation: 337
I have been following an example program from a tutorial book, the program is to take a dictionary with all 50 US states in and their capitals and then to create a random set of multiple choice A-D questions, these questions are then to be randomized and 3 different quizzes printed out into 3 different files. The answers for all the questions for each quiz are then to be printed out into an answers file to go with each questions file.
As a test Im only doing it with a range of 3 for now. When I run the program the files are created but only the 3rd one has the questions in its quiz file and only the 3rd answer file has its answers in too. Files 1 and 2 for the questions have the header section with the blank Name: and Date: but nothing else and their answer files are blank.
I have been over this several times now and can't figure out what the problem is.
Any input would be appreciated, thanks.
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',
'New Mexico': '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',
'West Virginia': 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}
# Generate quiz files
for quizNum in range(3):
# Create the quiz and answer key files.
quizFile = open('capitalsquiz%s.txt' % (quizNum + 1), 'w')
answerKeyFile = open('capitalsquiz_answers%s.txt' % (quizNum + 1), 'w')
# Write out the header for the quiz.
quizFile.write('Name:\n\nDate:\n\nPeriod:\n\n')
quizFile.write((' ' * 20) + 'State Capitals Quiz (Form %s)' % (quizNum + 1))
quizFile.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 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('%s. What is the capital of %s?\n' % (questionNum + 1, states[questionNum]))
for i in range(4):
quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions[i]))
quizFile.write('\n')
# Write the answer key to a file.
answerKeyFile.write('%s. %s\n' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)]))
quizFile.close()
answerKeyFile.close()
Upvotes: 0
Views: 7504
Reputation: 577
It's because first you iterate over [0,1,2]
and create all the files, and then, with quiz 3 open you iterate over writing the actual question/answers.
Put everything under one for questionNum in range(3)
.
EDIT: I see that it's because of you indentation, you exit the first for loop too soon. See https://gist.github.com/Noxeus/dcb3898f601ef76fbf8f
Upvotes: 1
Reputation: 187
I think everytime you call quizfile.write () ,it overwrites the previous text that was inserted in your file because you opened the file in w mode, try opening the file in append mode("a" in open() instead of "w"),it should work out.
Upvotes: 1
Reputation: 826
Think about the iteration of your loops and the setting of the variables.
Did you mean for the for questionNum in range(50):
to be a separate or inner loop to the for quizNum in range(3):
loop, this may be an issue of indentation within your pyton file.
When your for questionNum in range(50):
loop starts the value of quizFile
and answerKeyFile
are set to the last in the for quizNum in range(3):
hence the writing to only the last file. At the time the for questionNum in range(50):
loop starts the for quizNum in range(3):
has finished
To solve: Put your question making loop in your quiz file loop (indentation is the key)
for quizNum in range(3):
...
for questionNum in range(50):
Upvotes: 2