#!/usr/bin/perl -w # my quiz use strict; use CGI qw(param); my $max; my @question; my @correct; my @answer; my $i = "0"; my $j = "0"; my $k = "0"; my $score = "0"; my (@A, @B, @C, @D); my $who = param("name"); my $surname = param("surname"); open(QSTNS, "questions.txt") || die "cannot open questions.txt $!"; while (<QSTNS>) { $_ =~ /^(Quiz)(\d)$/; $max = $2; } close(QSTNS) || warn "cannot close questions.txt $!"; while ($k < $max) { push (@answer,param("answer$k")); $k++; } print <<Firstbit; Content-type: text/html <HTML><HEAD><TITLE>Quiz</TITLE></HEAD> <BODY><H1>WELCOME TO MY QUIZ!</H1> Firstbit if (param()) { open(QSTNS, "questions.txt") || die "cannot open questions.txt $!" +; while(<QSTNS>) { if ($_ =~ /^A\d:/) { chomp; s/A\d://; push (@correct,$_); } } close(QSTNS) || warn "cannot close questions.txt $!"; while ($j < $max) { if ($answer[$j] eq $correct[$j]) { $score++; #print "<P>score: $score"; } #print "<P>j: $j"; $j++; } #print "<P>@correct, @answer"; print "<p>$who $surname, you scored $score"; open(LOGS, ">>log.txt") || die "cannot append log.txt $!"; print LOGS "$who $surname scored $score\n"; close(LOGS); } else { open(QSTNS, "questions.txt") || die "<P>cannot open questions.txt +$!"; while(<QSTNS>) { if ($_ =~ /^Q\d/) { chomp; push (@question,$_); } if ($_ =~ /^C\d:/) { chomp; s/C\d://; $_ =~ /([^,]+),([^,]+),([^,]+),([^,]+)/; push (@A,$1); push (@B,$2); push (@C,$3); push (@D,$4); } } #print "<P>A: @A"; close(QSTNS); print <<Startform; <FORM> <P>Please enter your first name: <INPUT TYPE="TEXT" NAME="name"> <P>Please enter your surname: <INPUT TYPE="TEXT" NAME="surname"> Startform while ($i < $max) { print <<Loop; <P>$question[$i] <BR> <INPUT TYPE="RADIO" NAME="answer$i" VAL +UE="$A[$i]">$A[$i]<BR> <INPUT TYPE="RADIO" NAME="answer$i" VAL +UE="$B[$i]">$B[$i]<BR> <INPUT TYPE="RADIO" NAME="answer$i" VAL +UE="$C[$i]">$C[$i]<BR> <INPUT TYPE="RADIO" NAME="answer$i" VAL +UE="$D[$i]">$D[$i]<BR> Loop $i++; } print <<Endform <P> <INPUT TYPE="SUBMIT" VALUE="Send Answers"> <INPUT TYPE="RESET" VALUE="Clear Answers"> </FORM> Endform } print <<Endbit; </BODY></HTML> Endbit
#################################### #The questions.txt file should be formatted as follows ###################################### Quiz5 Q1. What is the capital of Afghanistan? C1:Tirana,Grozny,Kabul,Tblisi A1:Kabul Q2. What is the capital of Albania? C2:Tirana,Grozny,Kabul,Tblisi A2:Tirana Q3. What is the capital of Chechnya? C3:Tirana,Grozny,Kabul,Tblisi A3:Grozny Q4. How many balls on a snooker table at the break off? C4:15,21,22,25 A4:22 Q5. What's the nearest planet to the Sun? C5:Earth,Venus,Mercury,Pluto A5:Mercury
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multiple Choice Quiz
by spaz (Pilgrim) on Feb 13, 2001 at 03:35 UTC | |
|
Re: Multiple Choice Quiz
by repson (Chaplain) on Feb 13, 2001 at 12:49 UTC |