Description: matches the capitals of 50 states in the United States.
Here are the tasks the program needs to complete:
Create 35 different test papers.
Create 50 multiple choice questions for each test paper in random order.
Provide each question with a correct answer and 3 random wrong answers in random order.
• write test papers into 35 text files.
• write answers in 35 text files.
This means that the code needs to do the following:
Keep the States and their capitals in a dictionary.
• for the test text file and answer text file, call open(), write(), and close().
Use random.shuffle() to randomly adjust the order of problems and multiple options.
Code:
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 4 import random,os 5 #Set the storage path of test papers and answers 6 testPath='F:\\Test' 7 answerPath='F:\\Answer' 8 #Determine whether the path exists. If not, create a 9 if not os.path.exists(testPath): 10 os.mkdir(testPath) 11 if not os.path.exists(answerPath): 12 os.mkdir(answerPath) 13 #Save the test data in a dictionary. key For the state name, value Name of capital 14 capitals = {'Alabama': 'Montgomery', 'Alaska': 'quesNumuneau', 'Arizona': 'Phoenix', 15 'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver', 16 'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee', 17 'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', 'Illinois': 18 'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas': 19 'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', 'Maine': 20 'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston', 'Michigan': 21 'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'quesNumackson', 'Missouri': 22 'quesNumefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada': 23 'Carson City', 'New Hampshire': 'Concord', 'New quesNumersey': 'Trenton', 'NewMexico': 24 'Santa Fe', 'New York': 'Albany', 'North Carolina': 'Raleigh', 25 'North Dakota': 'Bismarck', 'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City', 26 'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence', 27 'South Carolina': 'Columbia', 'South Dakota': 'Pierre', 'Tennessee': 28 'Nashville', 'Texas': 'Austin', 'Utah': 'Salt Lake City', 'Vermont': 29 'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 'WestVirginia': 30 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'} 31 #Cycle 35 times to create 35 sets of test papers 32 for fileNum in range(35): 33 #os.path.quesNumoin:Connect the path and file name, and the corresponding file location can be determined. 34 quesFile=open(os.path.join(testPath,'question%s.txt' %(fileNum+1)),'w') 35 answerFile=open(os.path.join(answerPath,'answer%s.txt' %(fileNum+1)),'w') 36 #Write the title information of the test paper 37 quesFile.write('Name:\n\nDate:\n\nPeriod:\n\n') 38 quesFile.write((' '*20)+'state Capitals Quiz(Form %s)' %(fileNum+1)) 39 quesFile.write('\n\n') 40 #Write state name to a list 41 states=list(capitals.keys()) 42 #Use random.shuffle()Function to sort the contents of a list randomly 43 random.shuffle(states) 44 #Cycle to create 50 topics 45 for quesNum in range(50): 46 #Get the right answer 47 correctAnswer=capitals[states[quesNum]] 48 #Write all answers to a list 49 wrongAnswers=list(capitals.values()) 50 #Delete the correct answers in the list to get a list of all the wrong answers 51 del wrongAnswers[wrongAnswers.index(correctAnswer)] 52 #Get 3 answers randomly from the list of wrong answers 53 wrongAnswers=random.sample(wrongAnswers,3) 54 #Put the normal answer and 3 wrong answers into a list of options 55 answerOptions=wrongAnswers+[correctAnswer] 56 #Sort the list of options randomly 57 random.shuffle(answerOptions) 58 59 #Write title quesFile 60 quesFile.write('\n%s.What is the capital of %s?\n' %(quesNum+1,states[quesNum])) 61 #Write options to quesFile 62 for k in range(4): 63 quesFile.write('\n%s.%s' %('ABCD'[k],answerOptions[k])) 64 quesFile.write('\n') 65 66 #Write answers answerFile 67 answerFile.write('%2s.%s\n' %(quesNum+1,'ABCD'[answerOptions.index(correctAnswer)])) 68 quesFile.close() 69 answerFile.close()
Operation result: