Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use POSIX;
- #init stuff
- my @questions;
- my @answers;
- my @used;
- my $max = 5 || @questions;
- my $score = 0;
- my $filename = "questions.txt";
- open(my $file, "<", $filename) or die "cannot open $filename for reading: $!\n";
- while(<$file>) {
- my ($question, $answer) = split(/;\s/);
- push(@questions, $question);
- push(@answers, $answer);
- }
- close($file);
- quiz_and_stuff();
- sub get_question {
- my $num = floor(rand(@questions));
- my @tmp;
- while (grep {/$num/} @used) {
- $num = floor(rand(@questions));
- }
- push(@used, $num);
- return ($questions[$num], $answers[$num]);
- }
- sub quiz_and_stuff {
- while(@used != $max) {
- my ($question, $answer) = get_question();
- print ($question, "\n");
- my $input = <>;
- chomp $input;
- if($answer =~ /^$input$/i) {
- $score++;
- print "Correct!" . (@used == $max ? "End of quiz!\n\n" : " Next question!\n\n");
- }
- else {
- print "Nope!" . (@used == $max ? "End of quiz!\n\n" : " Next question!\n\n");
- }
- }
- print "Your score: $score/$max - " . ($score/$max)*100 . "%\n";
- }
Add Comment
Please, Sign In to add comment