how to show the number of the correct asnwer

A

angel

hello i am a perl beginner and i need help. i dont know and have no
idea on how to make a program for this,

the program should ask a user 5 questions. it should read the answers
and shows the number of answers that were right.

example:

Please enter £ two times: ££
Correct!

Please enter $ four times: ***
Wrong!

*****

You got 4 correct answers.

I have already written a program for the 5 questions:

e.g.

print ("Please enter £ two times: ");
$pound = <STDIN>;
$correct = "Correct!";

if ($pound !~m/££/)
{
print("Wrong!\n");
}
else
{
print("$correct\n");
}

print("Please enter \$ four times: ");
$dollar = <STDIN>;

if($dollar !~m/^(\${4})$/)
(
print("Wrong!\n");
}
else
{
print("$correct\n");
}

my problem is i dont know on how it will read the answers and shows the
number of answers that were right. i would really appreciate any help.
thanks!
 
J

Jim Gibson

angel said:
hello i am a perl beginner and i need help. i dont know and have no
idea on how to make a program for this,

the program should ask a user 5 questions. it should read the answers
and shows the number of answers that were right.
[example input, output, and code snipped]
my problem is i dont know on how it will read the answers and shows the
number of answers that were right. i would really appreciate any help.
thanks!

Perl variables can hold numbers and you can use them in arithmetic
expressions:

my $n_correct = 0;
...

$n_correct = $n_correct + 1;

which may be written less verbosely as:

$n_correct += 1;

or

$n_correct++;

Then:

print "The number of correct answers is: $n_correct\n";
 
J

John W. Krahn

angel said:
hello i am a perl beginner and i need help. i dont know and have no
idea on how to make a program for this,

the program should ask a user 5 questions. it should read the answers
and shows the number of answers that were right.

example:

Please enter £ two times: ££
Correct!

Please enter $ four times: ***
Wrong!

*****

You got 4 correct answers.

I have already written a program for the 5 questions:

e.g.

print ("Please enter £ two times: ");
$pound = <STDIN>;
$correct = "Correct!";

if ($pound !~m/££/)
{
print("Wrong!\n");
}
else
{
print("$correct\n");
}

print("Please enter \$ four times: ");
$dollar = <STDIN>;

if($dollar !~m/^(\${4})$/)
(
print("Wrong!\n");
}
else
{
print("$correct\n");
}

my problem is i dont know on how it will read the answers and shows the
number of answers that were right. i would really appreciate any help.

my @numbers = qw( zero one two three four five six seven eight nine ten eleven
twelve );
my @questions = ( [ qw/ £ 2 / ], [ qw/ $ 4 / ] );

my $correct = 0;
for my $question ( @questions ) {
print "Please enter $question->[0] $numbers[$question->[1]] times: ";
chomp( my $answer = <STDIN> );
print $answer eq ( $question->[0] x $question->[1] ) ? do { ++$correct;
"Correct!\n" } : "Wrong!\n";
}

print "You got $correct correct answer", $correct == 1 ? '' : 's', ".\n";





John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,051
Members
47,656
Latest member
rickwatson

Latest Threads

Top