python generate random test paper files in the specified folder

# ! Python3 
# generated in the specified path specified number of test paper and answer reference text, select the contents of the paper is to test the capital city of China's 28 provincial-level administrative regions, and 

# requires different questions each order of questions, in different papers the same questions randomly selected option. 

Import Random
 Import os 

# provincial capital and its data dictionary 
Capitals = { ' Xinjiang ' : ' Urumqi ' , ' Gansu ' : ' Lanzhou ' , ' Ningxia ' : ' Yinchuan ' , ' Shaanxi ' : ' Xi ' ,
             ' Qinghai ' :' Xining ' , ' Tibet ' : ' Lhasa ' , ' Guizhou ' : ' Guiyang ' , ' Inner Mongolia ' : ' Hohhot ' , 
             ' Shanxi ' : ' Pax ' , ' Shandong ' : ' Jinan ' , ' Henan ' : ' Zhengzhou ' , ' Hebei ': ' Shijiazhuang' , 
             ' Heilongjiang ' : ' Harbin ' , ' Jilin ' : ' Changchun ' , ' Liaoning ' : ' Shenyang ' , ' Jiangsu ' : ' Nanjing ' ,
             ' Anhui ' : ' Hefei ' , ' Sichuan ' : ' Chengdu ' , ' Fujian ' :' Fuzhou ', ' Hunan ' : ' Changsha ' ,
             ' Hubei ' : ' Wuhan ' , ' Guangdong ' : ' Guangzhou ' , ' Guangxi ' : ' Nanning ' , ' Yunnan ' : ' Kunming ' , 
             ' Hainan ' : ' the sea ' , ' Taiwan ' : 'Taipei ' ,' Zhejiang ' : ' Hangzhou ' , ' Jiangxi ' : ' Nanchang ' } 

# create a path in the D drive to store test questions 
quizePath = ' D: \\ test \\ geography exam \\ capital city library test library \\ ' 
IF  not os.path.exists (quizePath): 
    os.makdirs ( ' D: \\ test \\ geography exam capital city library \\ \\ test library ' ) 

# generate 49 parts of test questions and answers corresponding reference in the specified folder 

for quizeNum in Range (49 ):
     # generate questions and answers file 
    quizeFile = Open (quizePath + ' capital test .txt} { ' .format (+ quizeNum. 1),' W ' ) 
    answerKeyFile = Open (quizePath + ' capital test answer .txt} { ' .format (+ quizeNum. 1), ' W ' ) 
    
    # generates questions volume header 
    quizeFile.write ( ' Name: \ n \ n Date: \ n \ n cycles: \ n \ n ' ) 
    quizeFile.write ( '  ' * + 23 is ' of capital test questions (the first set {}) ' .format (+ quizeNum. 1 )) 
    quizeFile.write ( ' \ n \ n ' ) 
    
    # randomly arranged order provinces 
    Province = List (capitals.keys ()) 
    random.shuffle (Province) 
    
    forquestionNum in the Range (len (Capitals)):
         # create answers and options 
        correctAnswer = Capitals [Province [questionNum]] 
        wrongAnswers = List (capitals.values ())
         del wrongAnswers [wrongAnswers.index (correctAnswer)] 
        wrongAnswers = random.sample ( wrongAnswers, 3 ) 
        answerOptions = wrongAnswers + [correctAnswer] 
        random.shuffle (answerOptions) 
        
       # in the papers written questions and answer choices 
        quizeFile.write ( ' {0}, {1} the city is the capital? \ the n- ' .format ( questionNum + 1 ,
                                                  Province [questionNum])) 
        for I in Range (. 4 ): 
            quizeFile.write ( ' {0}, {}. 1 \ n- ' .format ( ' ABCD ' [I], answerOptions [I])) 
        quizeFile.write ( ' \ n- ' ) 
        
        # write the correct answer in the reference answer 
        answerKeyFile.write ( ' % S,% S \ n- ' % (+ questionNum. 1 ,
                             ' ABCD ' [answerOptions.index (correctAnswer)])) 
    
    # when every papers and suggested answers Close finish creating the appropriate file 
     quizeFile.close ()
    answerKeyFile.close ()

Guess you like

Origin www.cnblogs.com/shanger/p/12178055.html