Quizbank/Python/conceptual

From Wikiversity
Jump to navigation Jump to search

These codes reside in the path Quizbank/conceptual

input.txt[edit | edit source]

==Raw script==

:t QB/d_Bell.photon

:! q1 CCO (public domain) [[user:Guy vandegrift]]
:? If the wavelength ''λ'' associated with a photon is cut in half, the photon's energy ''E''
:- is cut in half
:- is reduced by a factor of 4
:- stays the same
:+ becomes twice as big
:- becomes 4 times as big
:$ ''c=fλ'' is easy to remember because the dimensions are right.  Since ''fλ'' is constant, ''f∝1/λ.  ''E=hf'' is harder to remember, but leads to ''E∝f'' leads to ''E∝1/λ''  If wavelength goes down, energy goes up porportionally.


:! q2 CCO (public domain) [[user:Guy vandegrift]]
:? If the wavelength ''λ'' associated with a photon doubles, the photon's frequency ''f''
:+ is cut in half
:- is reduced by a factor of 4
:- stays the same
:- becomes twice as big
:- becomes 4 times as big
:$ ''c=fλ'' is easy to remember because the dimensions are right.  Since ''fλ'' is constant, ''f∝1/λ. If wavelength doubles, frequency is cut in half.


:! q3 CCO (public domain) [[user:Guy vandegrift]]
:? If the frequency ''f'' associated  with a photon increases by a factor of 4, the photon's wavelength ''λ''
:- is cut in half
:+ is reduced by a factor of 4
:- stays the same
:- becomes twice as big
:- becomes 4 times as big
:$ ''c=fλ'' is easy to remember because the dimensions are right.  Since ''fλ'' is constant, ''f∝1/λ. If wavelength goes up a factor of 4, frequency goes down a factor of 4.


:! q4 CCO (public domain) [[user:Guy vandegrift]]
:? If the frequency ''f'' associated  with a photon increases by a factor of 4, the photon's energy ''E''
:- is cut in half
:- is reduced by a factor of 4
:- stays the same
:- becomes twice as big
:+ becomes 4 times as big
:$ Here all we need is the Plank relation between energy and frequency ''E=hf''


:! q5 CCO (public domain) [[user:Guy vandegrift]]
:? If an atom emits two photons in a cascade emission and both photons have 2 eV of energy, the atom's energy
:- stays the same
:- increases by 2 eV
:- increases by 4 eV
:- decreases by 2 eV
:+ decreases by 4 eV
:$ A cascade emission (at two different frequencies) is one way to do a Bell test with photons.


:! q6 CCO (public domain) [[user:Guy vandegrift]]
:? If an atom absorbs a photon with 2 eV energy, the atom's energy
:- stays the same
:+ increases by 2 eV
:- increases by 4 eV
:- decreases by 2 eV
:- decreases by 4 eV
:$ easy question


:! q7 CCO (public domain) [[user:Guy vandegrift]]
:? If a 3 eV photon strikes a metal plate and causes an electron to escape, that electron will have a kinetic energy that is
:- zero
:+ less than 3 eV
:- equal to 3 eV
:- greater than 3 eV
:- equal to 6 eV
:$ First the electron loses KE due to the work function.  But also, the stopping voltage measures only the component of KE associated with motion perpendicular to the plate.  It is the first consideration that guarantees an energy less than the photon's.


:! q8 CCO (public domain) [[user:Guy vandegrift]]
:? In the [[w:PhET Interactive Simulations|Phet lab]] for photoelectric effect, how was the electron's kinetic energy measured?
:- measuring spin
:- measuring polarization
:- measuring both spin and polarization
:- deflecting the electron with a magnetic field
:+ stopping the electron with an applied voltage
:$ The lab currently can be found at https://phet.colorado.edu/en/simulation/photoelectric


:! q9 CCO (public domain) [[user:Guy vandegrift]]
:? If an atom absorbs a photon with 4 eV energy, the atom's energy
:- stays the same
:- increases by 2 eV
:- increases by 4 eV
:- decreases by 2 eV
:+ decreases by 4 eV
:$ easy question perhaps too easy?


:! q10 CCO (public domain) [[user:Guy vandegrift]]
:? If 10<sup>18</sup> photons pass through a small hole in your roof every second, how many photons would pass through it if you doubled the diameter?
:- 10<sup>18</sup>
:- 2x10<sup>18</sup>
:+ 4x10<sup>18</sup>
:- 6x10<sup>18</sup>
:- 8x10<sup>18</sup>
:$ Area goes as radius squared (basic dimensional analysis says this even if you don't use A=πR<sup>2</sup>


:! q11 CCO (public domain) [[user:Guy vandegrift]]
:? Two black bodies of are created by cutting identical small holes in two large containers.  The holes are oriented so that all the photons leaving one will enter the other.  The objects have different temperature and different volume. Which object has the greater electromagnetic ("photon") energy density (energy per unit volume)?
:+ The hotter object has a greater energy density.
:- The larger object has a greater energy density.
:- They have the same energy density (since the holes are identical).
:- No unique answer exists because two variables are involved (temperature and volume).
:$ This question serves two purposes:  (1) to inform students that the "photon" first emerged as a solution to the blackbody problem, and (2) to introduce the distinction between [[w:intensive and extensive properties|intensive and extensive properties]].


:! q12 CCO (public domain) [[user:Guy vandegrift]]
:? Two black bodies of are created by cutting identical small holes two large containers.  The holes are oriented so that all the photons leaving one will enter the other.  The objects have different temperature and different volume. Which object emits more photons per second (above a given threshold energy)?
:+ The object with the greater temperature emits more.
:- The object with the greater volume.
:- They both emit the same number of photons (since the holes are identical).
:- No unique answer exists because two variables are involved (temperature and volume).
:$ We know that the emission spectrum of a black body depends only on temperature (with power also depending on area).  [[w:special:permalink/842328570#Carnot's_principle|Carnot's version of the second law of thermodynamics]] stipulates that photon energy must flow from the hotter to the colder object.  By inserting filters between the two (identical) holes we can ensure that this equality holds at all wavelengths.


:! q13 CCO (public domain) [[user:Guy vandegrift]]
:? Two black bodies of are created by cutting identical small holes in two large containers.  The holes are oriented so that all the photons leaving one will enter the other.  The objects have different temperature and different volume. Which object has the greater electromagnetic ("photon") energy?
:- The hotter object has a greater energy.
:- The larger object has a greater energy.
:- They have the same energy (since the holes are identical).
:+ No unique answer exists because two variables are involved (temperature and volume).
:$ To suppress blind memorization, this question is a partner to a similar one where the question was about energy ''density''.


:! q14 CCO (public domain) [[user:Guy vandegrift]]
:? [[File:Young Diffraction cropped.png|thumb|100px]] This figure is associated with 
:- Photons striking metal and ejecting electrons ([[w:Photoelectric effect|photo-electric effect]] explained in 1905)
:- Diffraction observed in light so faint that photons seemed to have no mechanism to interact with each other ([[w:special:permalink/841709261#Career_and_research|observed in 1909]])
:- A system similar to the one that led to the 1901 proposal that light energy is [[w:Planck's law|quantized as integral multiples of hf]] (except that Plank assumed that the walls were conductive.)
:+ Evidence presented in 1800 that [[w:Young's interference experiment|light is a wave]].
:- The transfer of energy and momentum of a high energy photon of a [[w:Compton effect|nearly free electron]].
:$ This is the first of four questions that might not suit all instructors.  One way to alleviate this is to look at whether a given question is on the test, and talk about that one.  Informing students that the others will not be on the test will prevent memorization, but not informing them (and not talking about) will also disincentivize memorization and encourage lecture attendance.  


:! q15 CCO (public domain) [[user:Guy vandegrift]]
:? [[File:Wave-particle duality static.svg|thumb|100px]] This figure is associated with 
:- Photons striking metal and ejecting electrons ([[w:Photoelectric effect|photo-electric effect]] explained in 1905)
:+ Diffraction observed in light so faint that photons seemed to have no mechanism to interact with each other ([[w:special:permalink/841709261#Career_and_research|observed in 1909]])
:- A system similar to the one that led to the 1901 proposal that light energy is [[w:Planck's law|quantized as integral multiples of hf]] (except that Plank assumed that the walls were conductive.)
:- Evidence presented in 1800 that [[w:Young's interference experiment|light is a wave]].
:- The transfer of energy and momentum of a high energy photon of a [[w:Compton effect|nearly free electron]].
:$ A candidate for the first "spooky" experiment of quantum mechanics?


:! q16 CCO (public domain) [[user:Guy vandegrift]]
:? [[File:Photoelectric_effect.svg|thumb|100px]] This figure is associated with 
:+ Photons striking metal and ejecting electrons ([[w:Photoelectric effect|photo-electric effect]] explained in 1905)
:- Diffraction observed in light so faint that photons seemed to have no mechanism to interact with each other ([[w:special:permalink/841709261#Career_and_research|observed in 1909]])
:- A system similar to the one that led to the 1901 proposal that light energy is [[w:Planck's law|quantized as integral multiples of hf]] (except that Plank assumed that the walls were conductive.)
:- Evidence presented in 1800 that [[w:Young's interference experiment|light is a wave]].
:- The transfer of energy and momentum of a high energy photon of a [[w:Compton effect|nearly free electron]].
:$ Students will likely memorize only one aspect of this answer.  I put the most important first (physical process), and expect other questions on other quizzes to reinforce that it is called the photo-electric effect.


:! q17 CCO (public domain) [[user:Guy vandegrift]]
:? [[File:Black-body_realization.png|thumb|100px]] This figure is associated with 
:- Photons striking metal and ejecting electrons ([[w:Photoelectric effect|photo-electric effect]] explained in 1905)
:- Diffraction observed in light so faint that photons seemed to have no mechanism to interact with each other ([[w:special:permalink/841709261#Career_and_research|observed in 1909]])
:+  A system similar to the one that led to the 1901 proposal that light energy is [[w:Planck's law|quantized as integral multiples of hf]] 
:- Evidence presented in 1800 that [[w:Young's interference experiment|light is a wave]].
:- The transfer of energy and momentum of a high energy photon of a [[w:Compton effect|nearly free electron]].
:$  Plank assumed that the walls were perfect conductors, not exactly what is shown.


:! q18 CCO (public domain) [[user:Guy vandegrift]]
:? A photon is polarized at 5° when it encounters a filter oriented at 35°.  What is the probability that it passes?
:-0
:-1/4
:-1/2
:+3/4
:-1
:$ <math>cos^2 30^\circ=3/4</math>


:! q19 CCO (public domain) [[user:Guy vandegrift]]
:? A photon is polarized at 10° when it encounters a filter oriented at 55°.  What is the probability that it passes?
:-0
:-1/4
:+1/2
:-3/4
:-1
:$ <math>cos^2 45^\circ=1/2</math>


:! q20 CCO (public domain) [[user:Guy vandegrift]]
:? A photon is polarized at 10° when it encounters a filter oriented at 70°.  What is the probability that it passes?
:-0
:+1/4
:-1/2
:-3/4
:-1
:$ <math>cos^2 60^\circ=1/4</math>


:! q21 CCO (public domain) [[user:Guy vandegrift]]
:? A photon is polarized at 10° when it encounters a filter oriented at 40°.  What is the probability that it is blocked?
:-0
:-1/4
:-1/2
:+3/4
:-1
:$ <math>cos^2 30^\circ=1/4</math> so it is blocked with P=3/4


:! q22 CCO (public domain) [[user:Guy vandegrift]]
:? A photon is polarized at 5° when it encounters a filter oriented at 50°.  What is the probability that it is blocked?
:-0
:-1/4
:+1/2
:-3/4
:-1
:$ <math>1-cos^2 45^\circ=1/2</math>


:! q23 CCO (public domain) [[user:Guy vandegrift]]
:? A photon is polarized at 5° when it encounters a filter oriented at 65°.  What is the probability that it is blocked?
:-0
:+1/4
:-1/2
:-3/4
:-1
:$ c<math>1-cos^2 30^\circ=1-3/4</math>


:! q24 CCO (public domain) [[user:Guy vandegrift]]
:? A photon is polarized at 10° when it encounters a filter oriented at 100°.  What is the probability that it passes?
:+0
:-1/4
:-1/2
:-3/4
:-1
:$ obvious


:! q25 CCO (public domain) [[user:Guy vandegrift]]
:? A photon is polarized at 10° when it encounters a filter oriented at 100°.  What is the probability that it is blocked?
:-0
:-1/4
:- 1/2
:- 3/4
:+ 1
:$ obvious

:z

python.py[edit | edit source]

This resides in Quizbank/concdptual

##This code reads input.txt and:
##  checks quizname (1st line) whether it is already in the bank
##  converts the convenient format to wmf quiz extension
##  places the proper headers and footers the quiz and the answer key
### Magic words indraft
##*_Name_*        >> bp_Name_
##*_Permalink_*   >> [[Special:Permalink/bp_Permalink_]]
##*_Attribution_* >> bp_Attribution_
##                   bp_quiz 

##It places the results in two textfiles under the name of the file.
## One textfile goes into QB and the other goes into the talk page
##Warning: if the name of the file contains a slash, the
##directory needs to be created.  For example QB/ probably needs
##to exist as a folder.
#I wrote this code to supplement Tom's python effort.  conceptual.py
#serves to convert a textfile that used magic words into a WMF quiz extension
#that is placed in w:QB/ subspace as a quizbank quiz.
#The input textfile could be easily written by students with minimal wikitext
#skill who need only master a few "magic words", which are always begin with a
# colon (:) so they look the same in wikitext as on a wikipage, they are:
#  [':t',':?',':!',':+',':-',':$',':z']

import os
import re #regular expression
import time
import shutil
#import random Don't need
import sys
import winsound #just messing around: This beeps if the quiz is not in the bank
  
### functions
#def checkTitle: verifies that I know if the quiz is already in the bank
#def stripBlanks: Tom's code that makes everything uniform by stripping
#                   unneede blanks from the wikitext. Also removes damaging
#                    newlines (/n) that can disrupt wikitext on wikis
#def splitline: Separates the two-symbol magic word prefix from the text written
#                 by students.
#def splitLine(string): Convenient(???) combination of previous two functions

def checkTitle(quizName):
  allFiles = os.listdir("../bank/")   # imagine you're one directory above test dir
  #print(*allFiles, sep='\n') #diagnostic
  #print(len(allFiles),' quizzes in bank') #diagnostic
  quizFileName=str(quizName)+'.txt'  #? do i need str her?
  answer=''
  while answer not in ['y','yes','Y','Yes','YES','n','no','N','NO']:
   answer=input('Is \"'+ quizName+ '.txt\" already in the bank?')
  if answer in ['y','yes','Y','Yes','YES']:
    if quizFileName in allFiles:
      print("It\'s your job to replace it.")
    else:
      winsound.Beep(440, 200)
      winsound.Beep(330, 200) 
      print('\a No! \"'+str(quizName)+'.txt \" is not in the bank.')
  if answer in ['n','no','N','NO']:
    if quizFileName not in allFiles:
      print('Continue with your new quiz')
    else:
      winsound.Beep(330, 200) 
      winsound.Beep(440, 200)
      print("\a Yes it is!")
#
# sripBlanks removes the first blanks from a string. Also removes newline.
def stripBlanks(string):
  #print('first',string)#diagnostic
  newString=[]
  rawLen = len(string)
  nstart=0 #default start place
  look=True # looking for blanks at start
  newString.append(string[0])
  newString.append(string[1])
  for n in  range(2,rawLen):
    #print('diag ',string[n],look)
    if look and string[n]==' ':#looking for spaces
      #print('found a space in ',n, ', look = ',look)#diagnostic
      nstart=n #iterates up until we stop looking
    else:
      look=False
      if not string[n]=='\n':
        newString.append(string[n])
  newLen=len(newString)
  return newString

# this program creates two strings from one
def splitLine(string):
  prefix=string[:2]
  body=string[2:len(string)]
  return [prefix,body]

#This combines the previous two programs.It take a line from the text
#and returns two strings: prefix and body
def formatLine(line):
  line=stripBlanks(line)
  joinedLine=''.join(line)
  [prefix,body]=splitLine(joinedLine)
  return [prefix,body]
  
  

# this combines both parts of a question (:! and :?)
def makeQuestion(ques1,ques2):
  part1='\n{<!--'+ques1+'-->'
  part2=ques2+'}\n'
  part1t='*<!--'+ques1+'-->'#t means talk part
  part2t=ques2+'\n'
  question=part1+part2
  questiont=part1t+part2t
  return [question, questiont]

#
#New program starts here
#
magicPrefixes = [':t',':?',':!',':+',':-',':$',':z']
numberQuestions=0#no questions have been completed
with open('input.txt', 'r') as fopen: #raw is the contents of input.txt
    raw = fopen.readlines()
    #print(raw) #diagnostic
#initialize
answers=''
inAnswers=False
numberOfQuestions=0
for line in raw:
  if line[:2] in magicPrefixes:
    line=stripBlanks(line)
    joinedLine=''.join(line)
    [prefix,body]=formatLine(line)
    #This code crashes if the first magic word does not open file
    if prefix==':t':
      quizName=body #only needed for last line
      fquiz=open(body+'.txt','w')
      fquiz.write(
      '===*_Quiz_*===\n<quiz>\n')
      ftalk=open(body+'_talk.txt','w')

      ftalk.write('''First to allow and display discussion of each
question, and second, to store the quiz in raw-script for.\n''')
      ftalk.write('==[['+body+']]==\n')
    #It also crashes if we see another ':t' prefix
    if prefix==':$':
      print('we found a comment')
      comment=body
    if prefix==':!':
      ques1 = body
      numberOfQuestions=numberOfQuestions+1     
    if prefix==':?':
      ques2 = body #we presume ques1 was previously made
      [question, questiont]=makeQuestion(ques1,ques2)
      fquiz.write(question)
      ftalk.write('==='+str(numberOfQuestions)+'===\n')
      ftalk.write(questiont)
      #print(question) #diagnostic
    if not inAnswers: #we look for the switch
      if prefix in [':-',':+']:
        inAnswers=True
    #after starting answers, we either add answer or terminate
    if inAnswers:
      if prefix==':-':
        answers=answers+'- '+body +'\n' #adds false answer
      if prefix==':+':
        answers=answers+'+ '+body +'\n' #adds true answer
      if not prefix in [':+',':-']:      #exits inAnswers       
        #also, if we were in answers and go out we can print:

        fquiz.write(answers)
        ftalk.write(answers)
        ftalk.write('\n')
        if prefix==':$':
          tstr='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
          tstr=tstr+tstr
          ftalk.write(tstr+comment+'\n<div style="clear:{{{1|both}}};"></div>\n')
        #print(answers) #diagnostic
        #initialize
        inAnswers=False
        answers=''
    if prefix==':z': #exit the loop
      break 
  #end of loop
fquiz.write('\n</quiz>')
fquiz.close()#close files
#next add the raw script to the talk part:
ftalk.write('\n==Raw script==\n')
for line in open('input.txt', 'r'):
    ftalk.write(line)
ftalk.close()
print(str(numberOfQuestions)+' questions created in '+quizName)