rand problem v2.0
From: Jeff Borders (jeff_at_jeffborders.com)
Date: 10/28/04
- Next message: Maxipoint Rep Office: "how summarise results from 2 hashes?"
- Previous message: Gunnar Hjalmarsson: "Re: Using rand problem"
- Next in thread: Gunnar Hjalmarsson: "Re: rand problem v2.0"
- Reply: Gunnar Hjalmarsson: "Re: rand problem v2.0"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: beginners@perl.org Date: Thu, 28 Oct 2004 13:05:21 -0400
Hello, (revised to include entire program, input file, and output)
I've written a simple quiz program and I've run into a problem with rand
and the integer it returns. It seems that it will eventually choose
either 0 or n+1 (I'm guessing) which blows up my array. I want to make
sure it chooses from the entire pool of questions. ie. Don't skip the
first and last array item. What suggestions can anyone give me?
Thanks- Jeff Borders (perl newbie)
### Sample Input File: korean.txt ###
One = Hana
First = Il
Two = Dool
Second = Yi
Three = Set
Third = Sahm
Four = Net
Fourth = Sa
### Sample Output ###
Question 20: Olgul Dwi Chaki
1. Back pivot kick to the up
2. Ridgehand
3. Roundhouse kick to the middle
4. Leg
press q to quit
Enter your answer: 1
Question 21: Son
Use of uninitialized value in concatenation (.) or string at ./korean.pl
line 58, <STDIN> line 21.
1.
2. Master Instructor
3. Middle section
4. Hand
press q to quit
Enter your answer:
### korean.pl ###
#!/usr/bin/perl
use warnings;
use strict;
my $number_of_questions=0;
my $random_question=0;
my $random_order=0;
my $i=0;
my $response=0;
my $total=0;
my $incorrect=0;
my $percent=0;
my @answer;
my @question;
my $answer=0;
my $question=0;
open DICT, '<', 'korean.txt' or die "Cannot open 'korean.txt' $!";
while (<DICT>) {
chomp;
$number_of_questions++;
($answer, $question) = split /\s*=\s*/;
$answer[$number_of_questions] = $answer;
$question[$number_of_questions] = $question;
}
close DICT;
open ERRFILE, '>', 'errfile.txt' or die "Cannot open 'errfile.txt' $!";
sub check_vars
{
print ERRFILE "Question Number: $number_of_questions\n";
print ERRFILE "Number of Questions: $number_of_questions\n";
print ERRFILE "Random Question: $random_question\n";
print ERRFILE "Random Order: $random_order\n";
print ERRFILE "i: $i\n";
print ERRFILE "Response: $response\n";
print ERRFILE "Total: $total\n";
print ERRFILE "Incorrect: $incorrect\n";
print ERRFILE "Percent: $percent\n";
print ERRFILE "Answer: $answer\n";
print ERRFILE "Question: $question\n";
}
do {
$total++;
$random_question = int(rand($number_of_questions));
$random_order = int(rand(4)); #returns an integer from 0-3.
$random_order++; #increment to get 1-4.
print "\n\nQuestion $total: ";
print "$question[$random_question]\n\n";
for $i (1..4) {
if ($i == $random_order) {
print "$i. $answer[$random_question]\n"; #print correct value from
array.
} else {
print "$i. $answer[int(rand($number_of_questions))]\n"; #print
incorrect value from array.
}
&check_vars();
}
print "\npress q to quit\n";
print "Enter your answer: ";
chomp($response=<STDIN>);
if ($response eq 'q') {
print "\n";
$total--;
#if ($total = 0) { abort; }
}
elsif ($response != $random_order) { print "-----> Answer is
$random_order <-----\n"; $incorrect++; } #print correct value from
array.
} until ($response eq 'q');
$percent=eval (($total-$incorrect)/$total)*100;
printf "Out of %2d questions, you missed %2d for a grade of %3d%%\n",
$total,$incorrect,$percent;
print "Total number of questions is $number_of_questions\n";
close(ERRFILE) || die "Couldn't close file properly";
- Next message: Maxipoint Rep Office: "how summarise results from 2 hashes?"
- Previous message: Gunnar Hjalmarsson: "Re: Using rand problem"
- Next in thread: Gunnar Hjalmarsson: "Re: rand problem v2.0"
- Reply: Gunnar Hjalmarsson: "Re: rand problem v2.0"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|