Jump to content

making a question and answer generator


Recommended Posts

Ok so here's my "evil plan" lol: I am working on making a test to help me study for the A+. The idea is that I want to make it modular so I can specify a battery of tests.

My bright idea was to make an ini file generator, since that has built functions and a method for sorting. The generator is mostly done, it's a basic one to make multiple choice questions. Here's the tricky bit: I want to be able to mark each answer with 1, 2, 3, and 4 so I can then have the test program grab the answers at random, but still relevant to each question, and not show the numbers so it's harder to tell which ones are which.

Example:

Q. Who founded Apple Computer, Inc?

1) Steve Ballmer

2)Steve Jobs and Steve Wozniak

3) Bill Gates and Steve Jobs

4) Tom Petty and the Heartbreakers

but have that be in a different order everytime I take the test, so I can't cheat even though I'm writing the test lol. I have that figured out as far as how it's organized with the questions, just need to figure out how to grab the questions and answers, but do it in a way that it's not seen in the test, so I can randomize them. So far I'm sure I can randomize the question order, but still not sure on how to keep it from repeating a question.

Here's my code so far for the ini generator, the Test portion I'll be working on as soon as I can think of a way to have it pick a question at random. Partially why I'm here :)

#include <GUIConstantsEx.au3> ;Constants for GUI Events
#include <WindowsConstants.au3>

Global $Answer, $Answer_input[4], $counter = 0, $Build_bttn, $Question, $Question_input


_Create_GUI()
Func _Create_GUI()

    ;To implement a reset function, make everything in the GUI first, THEN put all the case stuff inside
    ;_Main().
    HotKeySet("{F3}","_Main") ;A program reset switch, in case a function gets funky and doesn't process right.
    GUICreate("Question and Answer Generator Version 0.1",640,480)

    ;The code for adding questions and answers to the .ini file.
    $Question_input = GUICtrlCreateEdit("Question goes here",45,45, 250, 75)
    $Answer_input[0] = GUICtrlCreateInput("1)", 45, 150, 300, 20)
    $Answer_input[1] = GUICtrlCreateInput("2)", 45, 175, 300, 20)
    $Answer_input[2] = GUICtrlCreateInput("3)", 45, 200, 300, 20)
    $Answer_input[3] = GUICtrlCreateInput("4)", 45, 225, 300, 20)


    $Build_bttn = GUICtrlCreateButton("Add to File", 525,440)

_Main()
EndFunc
Func _Main()


    GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $Gui_Event_Close
            Exit
        Case $Build_bttn
        $Question = GUICtrlRead($Question_input)
        $Answer = GUICtrlRead($Answer_input[0]) & @LF & GUICtrlRead($Answer_input[1]) & @LF & GUICtrlRead($Answer_input[2]) & @LF & GUICtrlRead($Answer_input[3])
        If ($counter) < 10 Then
            $counter = $counter+1
            IniWrite("AlphaTest.ini","Questions","Question_" & $counter, $Question)
            IniWriteSection("AlphaTest.ini","Answer_" & $counter,$Answer)
        EndIf
EndSwitch
WEnd
EndFunc

I understand this is a complicated project, one I'm willing to share back with the community of course since the idea is to make it a simple generator and then just point the Test exe to the q&a file

Edited by hackersarchangel
Link to comment
Share on other sites

You could put you questions and answers into separate arrays and shuffle them. Here's a very simple method to shuffle an array I wrote the other day. You can find other methods

Func _ArrayRandomShuffle(ByRef $aArray)
    Local $iRandom, $iBound = UBound($aArray) -1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc

Actually I realise this function is the same as that posted by jchd in the thread linked to above.

Edited by czardas
Link to comment
Share on other sites

Just for the fun of it, I wrote a version of your script. Maybe it will give you a different perspective. For my scirpt, I used just numbers for the section names and values in the .ini file. I think that will make it easier to work with randomly picking questions and answers.

I will try to work on a console for asking you questions. This one is just for adding questions to the ini file.

#include <GUIConstants.au3>

$iniFile = @ScriptDir & "AlphaTest.ini"
$Form1_1 = GUICreate("Form1", 679, 365)
$Edit1 = GUICtrlCreateEdit("", 32, 32, 537, 105)
$Label1 = GUICtrlCreateLabel("Enter your question:", 32, 8, 98, 17)
$Label2 = GUICtrlCreateLabel("Answer pool:", 32, 168, 65, 17)
$Input1 = GUICtrlCreateInput("", 32, 192, 537, 21)
$Input2 = GUICtrlCreateInput("", 32, 224, 537, 21)
$Input3 = GUICtrlCreateInput("", 32, 256, 537, 21)
$Input4 = GUICtrlCreateInput("", 32, 288, 537, 21)
$Button1 = GUICtrlCreateButton("Save Question", 272, 328, 115, 25)
$Label3 = GUICtrlCreateLabel("Correct Answer", 584, 160, 76, 17)
$Radio1 = GUICtrlCreateRadio("", 584, 192, 113, 17)
$Radio2 = GUICtrlCreateRadio("", 584, 224, 113, 17)
$Radio3 = GUICtrlCreateRadio("", 584, 256, 113, 17)
$Radio4 = GUICtrlCreateRadio("", 584, 288, 113, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _WriteToIni()
    EndSwitch
WEnd

Func _WriteToIni()
    Local $error = 0
    
    $question = GUICtrlRead($Edit1)
    $ans1 = GUICtrlRead($Input1)
    $ans2 = GUICtrlRead($Input2)
    $ans3 = GUICtrlRead($Input3)
    $ans4 = GUICtrlRead($Input4)
    $readradio1 = GUICtrlRead($Radio1)
    $readradio2 = GUICtrlRead($Radio2)
    $readradio3 = GUICtrlRead($Radio3)
    $readradio4 = GUICtrlRead($Radio4)
    
    If $readradio1 = 1 Then
        $correctAns = $ans1
    ElseIf $readradio2 = 1 Then
        $correctAns = $ans2
    ElseIf $readradio3 = 1 Then
        $correctAns = $ans3
    ElseIf $readradio4 = 1 Then
        $correctAns = $ans4
    Else
        MsgBox(0, "Error", "Please select which answer is correct")
        $error = 1
    EndIf
    
    If $error <> 1 Then
        $sectionNames = IniReadSectionNames($iniFile)
        $nextQuestion = UBound($sectionNames) - 1
        If $nextQuestion < 1 Then
            $nextQuestion = 0
        EndIf
        IniWrite($iniFile, $nextQuestion, "Question", $question)
        IniWrite($iniFile, $nextQuestion, "1", $ans1)
        IniWrite($iniFile, $nextQuestion, "2", $ans2)
        IniWrite($iniFile, $nextQuestion, "3", $ans3)
        IniWrite($iniFile, $nextQuestion, "4", $ans4)
        IniWrite($iniFile, $nextQuestion, "Correct", $correctAns)
    EndIf
EndFunc
Edited by sleepydvdr

#include <ByteMe.au3>

Link to comment
Share on other sites

I modified the previous script, so be sure to use the newer version. Here's the console to be asked the questions:

#include <GUIConstants.au3>
#include <Array.au3>

Dim $aArray[4]
$iniFile = @ScriptDir & "AlphaTest.ini"
Global $question, $correctAns, $error = 0

_GetQuestion()

$Form1 = GUICreate("Study Helper", 633, 341, 193, 125)
$Label1 = GUICtrlCreateLabel($question, 32, 48, 572, 73)
$Label2 = GUICtrlCreateLabel("Question:", 24, 16, 49, 17)
$Radio1 = GUICtrlCreateRadio($aArray[0], 40, 168, 545, 17)
$Radio2 = GUICtrlCreateRadio($aArray[1], 40, 192, 545, 17)
$Radio3 = GUICtrlCreateRadio($aArray[2], 40, 216, 545, 17)
$Radio4 = GUICtrlCreateRadio($aArray[3], 40, 240, 545, 17)
$Button1 = GUICtrlCreateButton("Submit", 264, 288, 75, 25, 0)
GUISetState(@SW_SHOW)



While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _CheckAnswer()
            $error = 0
    EndSwitch
WEnd

Func _GetQuestion()
    $namesOfSections = IniReadSectionNames($iniFile)
    $numberOfQuestions = $namesOfSections[0]
    $randomNumber = Random(0, $numberOfQuestions - 1, 1)
    $question = IniRead($iniFile, $randomNumber, "Question", "")
    $aArray[0] = IniRead($iniFile, $randomNumber, "1", "")
    $aArray[1] = IniRead($iniFile, $randomNumber, "2", "")
    $aArray[2] = IniRead($iniFile, $randomNumber, "3", "")
    $aArray[3] = IniRead($iniFile, $randomNumber, "4", "")
    $correctAns = IniRead($iniFile, $randomNumber, "Correct", "")
    Local $iRandom, $iBound = UBound($aArray) -1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc

Func _CheckAnswer()
    If GUICtrlRead($Radio1) = 1 Then
        $answer = $aArray[0]
    ElseIf GUICtrlRead($Radio2) = 1 Then
        $answer = $aArray[1]
    ElseIf GUICtrlRead($Radio3) = 1 Then
        $answer = $aArray[2]
    ElseIf GUICtrlRead($Radio4) = 1 Then
        $answer = $aArray[3]
    Else
        MsgBox(0, "Error", "You did not select anything")
        $error = 1
    EndIf
    
    If $error <> 1 Then
        If $answer = $correctAns Then
            MsgBox(0, "Correct", "You got the question correct!")
        Else
            MsgBox(0, "Incorrect", "Sorry, wrong answer. The correct answer is:" & @CRLF & @CRLF & $correctAns)
        EndIf
    EndIf
    
    _ResetQuestion()
EndFunc

Func _ResetQuestion()
_GetQuestion()    
GUICtrlSetData($Label1, $question)
GUICtrlSetData($Radio1, $aArray[0])
GUICtrlSetData($Radio2, $aArray[1])
GUICtrlSetData($Radio3, $aArray[2])
GUICtrlSetData($Radio4, $aArray[3])
EndFunc

#include <ByteMe.au3>

Link to comment
Share on other sites

The first script I posted creates the ini file. This is what I used for testing purposes:

[0]
Question=How many days are in a week?
1=4
2=8
3=7
4=3
Correct=7
[1]
Question=What color is the sky?
1=Blue
2=Red
3=Green
4=Purple
Correct=Blue
[2]
Question=How old are you?
1=24
2=99
3=88
4=6
Correct=24

#include <ByteMe.au3>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...