♠/Creating numerical quizzes with Python

From Wikiversity
<
Jump to navigation Jump to search
Quizbank images are easy to make and upload to commons.

A long solenoid has a length 0.75 meters, radius 3.1 cm, and 500 turns. It surrounds coil of radius 5.9 meters and 10 turns. If the current in the solenoid is changing at a rate of 200 A/s, what is the emf induced in the surounding coil?

      a) 1.085E-02 V       b) 1.194E-02 V       c) 1.313E-02 V       d) 1.445E-02 V       e) 1.589E-02 V

I am looking for an easy way to create create numerical quizzes for Quizbank using Python (See also Quizbank/Python). As I develop this code, I will focus on Examples from OpenStax University Physics.


Study Guide (sample)[edit]

Test1[edit]

1/4 from Special:Permalink/1863117 to QB/a04DynForce Newton_sled
1/6 from Special:Permalink/1863376 to QB/b_busyBeaver
1/3 from Special:Permalink/1889654 to d_cp2.14

Test2[edit]

1/3 from Special:Permalink/1863132 to QB/a08linearMomentumCollisions
1/3 from Special:Permalink/1889654 to d_cp2.14

Journal[edit]

Found error in first quiz that I transcribed[edit]

Example 14.1 (Volume 2) at: https://cnx.org/contents/eg-XcBxE@9.7:H8S6dNUY@2/141-Mutual-Inductance

A long solenoid has a length 0.75 meters, radius 3.1 cm, and 500 turns. It surrounds coil of radius 5.9 meters and 10turns. If the current in the solenoid is changing at a rate of 200 A/s, what is the emf induced in the surounding coil?

The books says the mutual inductance M is 2.53E−5, but I calculate 7.95E−5:

0.75= len1.v=length
1.25664E-06= munaught=4*math.pi*10**(-7)
500= N1.v =turns
10= N2.v =turns
0.031= R1m=R1(meters)
3.01907E-03= math.pi*R1m**2=area sq meters
M=munaught * N1.v * N2.v * math.pi *area / len1.v
7.94588E-05= =M
***Something is wrong***
Mbook (books answer)= 2.53e-05
wrong by factor 0.3184041966024768

Python code[edit]

This code writes multiple renditions (with random numbers) for one numerical quiz question.[1]

Extended content
#Creates one quiz question with random (numerical) parameters

#First we import some modules that we will need:
import os, csv, re, time, shutil, random, sys, math
from numericalFunctions import roundSigFig, express, QuesVar, makeAnswers
random.seed() #needed to make random numbers more "random"
_R1=True #I have introduced this "magic" variable with the underscore
#   to ensure that nobody accidentally uses and changes it.
#   _R1=first rendition. Other reserved variables begin with "question"
#   Question:  Is this underscore use good or bad programming style?

### HEADER
#A header MUST be included as a hidden statement that begins each question
#One purpose is to attribute (for OpenStax we use the URL)
questionHeader= 'Example 14.1 from OpenStax University Physics 2: '\
'https://cnx.org/contents/eg-XcBxE@9.7:H8S6dNUY@2/141-Mutual-Inductance'

### RENDITION
#Define number of renditions (typically 20 or so:)
questionRenditions=3 

#These statements require no editing or modification:
questionQuestion=''#initial value
questionRenditionCount=0#initial value
questionName = os.path.basename(__file__)[:-3]
answerList=[]
questionList=[]
print(questionName+'\n' + questionHeader+'\n')
questionFile=open(questionName+'.txt','w')
questionFile.write('_name_'+questionName)
questionFile.write('\n_head_'+questionHeader+'\n')

### DECLARE VARIABLES
#Declare variables that are used both as text on the quiz, as well as
#numbers in calculating the answers. The first is taken from OpenStax
#Physics, the other two define the range of randomly selected values.
#The "magic" _R1 tells us to randomize if it is not the first rendition.

for questionRenditionCount in range(1,1+questionRenditions):  
    len1=QuesVar(.75, .51, .99, _R1)#length of long solenoid meters
    R1=QuesVar(3.10, 3.01, 4.99, _R1)#solenoid's radius
    N1=QuesVar(500, 501, 599, _R1)#solenoid's turns
    R2=QuesVar(5.90, 5.71, 9.99, _R1)#coil's radius
    N2=QuesVar(10,9,19, _R1)#coil's turns. 
    aps=QuesVar(200,101,299, _R1)#amps per second in solenoid
    
    #Write the question as one long string of wikitext
    #Use <br> for linebreaks, < math>..< /math> for equations, etc.
    #Images from commons are written e.g. as [[File:sample.png]]
    # The .t method  converts the numbers into printable strings.
    #Backslash \ tells continues  statement to next line:
       
    #Question
    print('Rendition '+str(questionRenditionCount)+': ')
    questionFile.write('_R_'+str(questionRenditionCount)+'\n')
    questionQuestion=\
    'A long solenoid has a length ' +len1.t+ ' meters, radius '\
    +R1.t + ' cm, and ' +N1.t+ ' turns.  It surrounds  coil of radius '\
    +R2.t+ ' meters and ' +N2.t+ 'turns.  If the current in the solenoid '\
    'is changing at a rate of '+aps.t+' A/s, what is the emf '\
    'induced in the surounding coil?'
    ###Solution: Here we replace the .t "text" method for printing 
    #             with the .v "variable" method for calculating.
    print(questionQuestion)
    questionFile.write('_Q_'+questionQuestion+'\n')

    ### SOLUTION
    munaught=4*math.pi*10**(-7)# Also works: math.pi*4E-7
    #PRINTING STEPS HELPS ME COMPOSE SOLUTION (MAY COMMENT OUT LATER)
    print(express(len1.v),'len1.v=length') 
    print(express(munaught),'munaught=4*math.pi*10**(-7)')
    print(express(N1.v),'N1.v =turns')
    print(express(N2.v),'N2.v =turns')
    R1m=R1.v/100 #must convert from cm to meters in this problem
    print(express(R1m),'R1m=R1(meters)')
    area=math.pi*R1m**2
    print(express(area),'math.pi*R1m**2=area sq meters')
    M=munaught * N1.v * N2.v * math.pi *area / len1.v
    emf=M*aps.v

    ### Typically end solution by defining questionAnswer:
    questionAnswer=emf
#### Aside Begin: the textbook seemed to be wrong    
    print('M=munaught * N1.v * N2.v * math.pi *area / len1.v')
    print(express(M), '=M')
    if _R1: #i.e. if the textbooks values are selected (1st rendition)
        print('***Something is wrong***')
        Mbook=2.53E-5 #from the textbook's Example 14.1
        print('Mbook (books answer)=',Mbook)
        print('wrong by factor',Mbook/M) #
        print('\n Next print a couple randomized versions:')
        print(  'Answer: ', express( roundSigFig(questionAnswer,3) )  )
#### Aside End                           
    #ordinarily this last line would define questionAnswer
      

    questionPre=''
    questionPost='&nbsp;UNITS' #&nbsp; is tag for no-break space
    print('Answer: '+'{0:1.5E}'.format(questionAnswer) )
    print('write:' + questionPre+ '###' + questionPost)
    answerString=makeAnswers(questionPre,
                         questionAnswer,questionPost, 1.1)
### Add  UNITS to answer and print
    answerString= answerString

    print(answerString)
    questionFile.write(answerString)
    _R1=False #Loops back with randoms

questionFile.close() #This text file will be later called to create quiz

Text output[edit]

All the renditions of a question are stored as a text file with magic words that help python create exams and study guides for courses.

Extended content

_name_cp2e.14.1-protected
_head_Example 14.1 from OpenStax University Physics 2: https://cnx.org/contents/eg-XcBxE@9.7:H8S6dNUY@2/141-Mutual-Inductance
_R_1
_Q_[[File:Coil and solenoid.svg|thumb|120px]]A long solenoid has a length 0.75 meters, radius 3.1 cm, and 500 turns.  It surrounds  coil of radius 5.9 meters and 10turns.  If the current in the solenoid is changing at a rate of 200 A/s, what is the emf induced in the surounding coil?
-a) 1.194E-02 V
-b) 1.313E-02 V
-c) 1.445E-02 V
+d) 1.589E-02 V
-e) 1.748E-02 V
_R_2
_Q_[[File:Coil and solenoid.svg|thumb|120px]]A long solenoid has a length 0.899 meters, radius 3.53 cm, and 599 turns.  It surrounds  coil of radius 9.15 meters and 10turns.  If the current in the solenoid is changing at a rate of 281 A/s, what is the emf induced in the surounding coil?
-a) 2.174E-02 V
-b) 2.391E-02 V
-c) 2.631E-02 V
+d) 2.894E-02 V
-e) 3.183E-02 V
_R_3
_Q_[[File:Coil and solenoid.svg|thumb|120px]]A long solenoid has a length 0.652 meters, radius 3.91 cm, and 567 turns.  It surrounds  coil of radius 8.34 meters and 10turns.  If the current in the solenoid is changing at a rate of 293 A/s, what is the emf induced in the surounding coil?
-a) 4.392E-02 V
+b) 4.831E-02 V
-c) 5.314E-02 V
-d) 5.846E-02 V
-e) 6.430E-02 V


Screen output[edit]

The output is a textfile that will be used to assemble quizzes. This screen output is used for diagnostic purposes.

Extended content
RESTART: C:\Users\User\18-6\QuizSoftware\numerical\cp2e.14.1.py ======
cp2e.14.1
Example 14.1 from OpenStax University Physics 2: https://cnx.org/contents/eg-XcBxE@9.7:H8S6dNUY@2/141-Mutual-Inductance

Rendition 1: 
A long solenoid has a length 0.75 meters, radius 3.1 cm, and 500 turns.  It surrounds  coil of radius 5.9 meters and 10turns.  If the current in the solenoid is changing at a rate of 200 A/s, what is the emf induced in the surounding coil?
0.75 len1.v=length
1.26E-06 munaught=4*math.pi*10**(-7)
500 N1.v =turns
10 N2.v =turns
0.031 R1m=R1(meters)
3.02E-03 math.pi*R1m**2=area sq meters
M=munaught * N1.v * N2.v * math.pi *area / len1.v
7.95E-05 =M
***Something is wrong***
Mbook (books answer)= 2.53e-05
wrong by factor 0.3184041966024768

 Next print a couple randomized versions:
Answer:  0.0159
Answer: 1.58918E-02
write:###&nbsp;UNITS
-a) 1.445E-02&nbsp;UNITS
+b) 1.589E-02&nbsp;UNITS
-c) 1.748E-02&nbsp;UNITS
-d) 1.923E-02&nbsp;UNITS
-e) 2.115E-02&nbsp;UNITS

Rendition 2: 
A long solenoid has a length 0.912 meters, radius 3.39 cm, and 584 turns.  It surrounds  coil of radius 8.7 meters and 10turns.  If the current in the solenoid is changing at a rate of 253 A/s, what is the emf induced in the surounding coil?
0.912 len1.v=length
1.26E-06 munaught=4*math.pi*10**(-7)
584 N1.v =turns
10 N2.v =turns
0.0339 R1m=R1(meters)
3.61E-03 math.pi*R1m**2=area sq meters
M=munaught * N1.v * N2.v * math.pi *area / len1.v
9.13E-05 =M
Answer: 2.30913E-02
write:###&nbsp;UNITS
-a) 2.099E-02&nbsp;UNITS
+b) 2.309E-02&nbsp;UNITS
-c) 2.540E-02&nbsp;UNITS
-d) 2.794E-02&nbsp;UNITS
-e) 3.073E-02&nbsp;UNITS

Rendition 3: 
A long solenoid has a length 0.767 meters, radius 4.19 cm, and 510 turns.  It surrounds  coil of radius 6.29 meters and 11turns.  If the current in the solenoid is changing at a rate of 144 A/s, what is the emf induced in the surounding coil?
0.767 len1.v=length
1.26E-06 munaught=4*math.pi*10**(-7)
510 N1.v =turns
11 N2.v =turns
0.04190000000000001 R1m=R1(meters)
5.52E-03 math.pi*R1m**2=area sq meters
M=munaught * N1.v * N2.v * math.pi *area / len1.v
1.59E-04 =M
Answer: 2.29334E-02
write:###&nbsp;UNITS
-a) 1.895E-02&nbsp;UNITS
-b) 2.085E-02&nbsp;UNITS
+c) 2.293E-02&nbsp;UNITS
-d) 2.523E-02&nbsp;UNITS
-e) 2.775E-02&nbsp;UNITS

>>>

Found another error in figure 14.12: LR circuit with switches.svg

Footnotes[edit]

  1. For more code see Quizbank/Python