Jump to content


Photo

[AHK_L] CSV Quiz


  • Please log in to reply
9 replies to this topic

#1 Frankie

Frankie
  • Members
  • 2930 posts

Posted 05 December 2010 - 04:43 PM

Heres a multiple choice quiz program I made. You put the answers in a CSV and it asks the questions.
FilePath := "Test.csv"
Title := "Test Quiz"
Questions := Object(), QCount := 0
Loop, Read, % FilePath
{
	Line := A_Index
	Loop, Parse, A_LoopReadLine, CSV
	{
		Item := A_Index
		If (Item == 1)
			Questions[Line,"Q"] := A_LoopField
		Else If (Item == 2)
			Questions[Line,"a"] := A_LoopField
		Else If (Item == 3)
			Questions[Line,"b"] := A_LoopField
		Else If (Item == 4)
			Questions[Line,"c"] := A_LoopField
		Else If (Item == 5)
			Questions[Line,"d"] := A_LoopField
		Else If (Item == 6)
			Questions[Line,"S"] := A_LoopField
	}
	QCount++
}
Gui, Font, s12
Gui, Add, Text, w315 vQText
Gui, Font
Gui, Add, Radio, vA Group w300
Gui, Add, Radio, vB w300
Gui, Add, Radio, vC w300
Gui, Add, Radio, vD w300
Gui, Font, s13
Gui, Add, Button, gNext, Next
QNum := 0 ;The current question that will be shown
QCorrect := 0
GoSub, Next
Gui, Show,, % Title
return

Next:
If (QNum)
	GoSub, CheckAnswer
QNum++
If (QNum > QCount)
{
	GoSub, Done
	return
}
GuiControl, Text, QText, % Questions[QNum,"Q"]
GuiControl, Text, A, % Questions[QNum,"a"]
GuiControl, Text, B, % Questions[QNum,"b"]
GuiControl, Text, C, % Questions[QNum,"c"]
GuiControl, Text, D, % Questions[QNum,"d"]
return

CheckAnswer:
Gui, Submit, NoHide
Correct := Questions[QNum,"S"]
If (%Correct% = True) ;For example, If (c = true)
	QCorrect++
GuiControl,, A, 0
GuiControl,, B, 0
GuiControl,, C, 0
GuiControl,, D, 0

return

Done:
Msgbox You got %QCorrect% out of %QCount%!
ExitApp
return
And an example CSV file. The format is question, answer1, answer2, answer3, answer4, correct answer (letter). It only currently works with four options.
"What is 1+1?","7","32","2","who cares...","c"
"Spell ""The"".","the","what?","no thanks","no!","a"
"What is the answer to life, the universe and everything?","Pie", "7", "idk","42!","d"


#2 Deathzilla7

Deathzilla7
  • Members
  • 11 posts

Posted 06 December 2010 - 05:46 AM

I happened to finish what I was doing, so I had a few mins to check back to it before bed.

Any chance you can tell me what a CSV is, how to use it, and how to program my own CSV quiz?

I don't want to just take your stuff, I want to KNOW how it works.

(Back by Wednesday)

#3 Frankie

Frankie
  • Members
  • 2930 posts

Posted 06 December 2010 - 07:33 PM

CSV is a file format for plain text that holds lists of data. The easiest ways to write CSV files are in a text editor or in excel. If you go into excel (or spreadsheet) you can make CSV files. The format is in the above post, but I'll say it again.
[color=olive]Question[/color], [color=orange]Answer1[/color], [color=orange]Answer2[/color], [color=orange]Answer3[/color], [color=orange]Answer4[/color], [color=darkred]Correct_Answer[/color]

This is what the example CSV file looks like in Excel:
Posted Image

More info -> <!-- m -->http://en.wikipedia....eparated_values<!-- m -->

#4 Deathzilla7

Deathzilla7
  • Members
  • 11 posts

Posted 09 December 2010 - 06:23 PM

I'll definitely have to test this over the weekend.

Could you possibly tell me which of the items you typed is a command, so I can look them all up and their parameters?

#5 Frankie

Frankie
  • Members
  • 2930 posts

Posted 09 December 2010 - 07:36 PM

Could you possibly tell me which of the items you typed is a command, so I can look them all up and their parameters?

If you mean in the script, I used a only a few commands.
I used Gui commands to create the gui and "Gui, Submit" to get the inputed data.
I also used GuiControl to change the text in controls of the gui. After each question I updated the text with the new question and set all the radios to off.
Gosub tells the program to go to a different point in the scirpt. I used it to go to the label Next and CheckAnswer.
Also, in the Done label, it puts a MsgBox on the screen and when it closes it calls ExitApp which as the name implies, closes the program.

#6 Deathzilla7

Deathzilla7
  • Members
  • 11 posts

Posted 12 December 2010 - 08:00 PM

So, Gosub can actually make the script stop intermediary subs and skip them to the specified one?

Like in BASIC?

But, then Filepath := means?, Title := means?, Questions := Object(), QCount := 0 means? , and Loop, Read, % FilePath means?

#7 Frankie

Frankie
  • Members
  • 2930 posts

Posted 12 December 2010 - 08:48 PM

So, Gosub can actually make the script stop intermediary subs and skip them to the specified one?

Like in BASIC?

GoSub goes to the named place in the code and runs the code from the label on. When it runs a return statement in that label it goes to the line after the "GoSub, Label" line. And yes, it is a command in basic.

But, then Filepath := means?, Title := means?

These are variables. I like to put them at the top of a script for editing convenience. If you want to change the file path that the quiz is stored at, or the title of your quiz window, all you have to do is change the text on these lines.

Questions := Object()

This initiates an object. In this case we use it as an array. The object/array is called Questions.

QCount := 0 means?

QCount is short for Question Count. It holds the number of questions found in the file. We use it later to check when the test is done. The line in the loop that says "QCount++" takes QCount and adds one to it.

and Loop, Read, % FilePath means?

Loop (read file contents)

#8 Deathzilla7

Deathzilla7
  • Members
  • 11 posts

Posted 14 December 2010 - 10:25 PM

I copied some of what you did and just changed names of files and stuff as a test, but I get that:
Questions[Line,"Q"] := A_LoopField
does not contain a recognized action?

BTW, what is A_LoopField?

#9 Frankie

Frankie
  • Members
  • 2930 posts

Posted 14 December 2010 - 10:32 PM

If you are using AHK Basic or and old build of AHK_L you can't run this. Download the latest from the download page.

BTW, what is A_LoopField?

Try looking in the help file...
Just type in any built-in variable, function or command to index and it will do its best to find it.

#10 MmcRob

MmcRob
  • Members
  • 1 posts

Posted 05 January 2012 - 05:12 PM

H There;
Firstly, Great Script! Works really well.

I am new to AUtohotkey_L have used the Basic one for a couple of years but never before needed to use any GUI's.

I am attempting to utilise your code to work with the following added functionality;

1) Instead of clicking the radio options with a mouse - The user would simply click a button on a Buzz controller ( Joypad 5-2 being option A-D (Due to layout of buttons) and Button 1 would simulate the selection of Next.

As i'm new to using GUI controls I was hoping someone could offer some help on this :roll: