# ♠/Creating numerical quizzes with Python

<
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)

### Test1

1/4 from Special:Permalink/1863117 to QB/a04DynForce Newton_sled

## Journal

### Found error in first quiz that I transcribed

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***
wrong by factor 0.3184041966024768
```

### Python code

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?

#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]
questionList=[]
questionFile=open(questionName+'.txt','w')
questionFile.write('_name_'+questionName)

### 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
N1=QuesVar(500, 501, 599, _R1)#solenoid's turns
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:
#### 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('wrong by factor',Mbook/M) #
print('\n Next print a couple randomized versions:')
#### Aside End
#ordinarily this last line would define questionAnswer

questionPre=''
questionPost='&nbsp;UNITS' #&nbsp; is tag for no-break space
print('write:' + questionPre+ '###' + questionPost)

_R1=False #Loops back with randoms

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

### Text output

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

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:### UNITS -a) 1.445E-02 UNITS +b) 1.589E-02 UNITS -c) 1.748E-02 UNITS -d) 1.923E-02 UNITS -e) 2.115E-02 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:### UNITS -a) 2.099E-02 UNITS +b) 2.309E-02 UNITS -c) 2.540E-02 UNITS -d) 2.794E-02 UNITS -e) 3.073E-02 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:### UNITS -a) 1.895E-02 UNITS -b) 2.085E-02 UNITS +c) 2.293E-02 UNITS -d) 2.523E-02 UNITS -e) 2.775E-02 UNITS

>>>

Found another error in figure 14.12:

## Footnotes

1. For more code see Quizbank/Python