#!/usr/bin/perl -w
use strict;
use Chemistry::Elements qw/get_name get_Z get_symbol/;
my $test_type = shift || "";
my %tests = (
name => \&name_test,
number => \&number_test,
symbol => \&symbol_test, );
if (exists $tests{$test_type}) {
$tests{$test_type}->();
} else {
error("Invalid Test type. Choose 'name', 'number', or 'symbol'.");
}
sub name_test {
while (1) {
my $number = int(rand 109) + 1;
my $symbol = get_symbol($number);
my $name = get_name($number);
print "Element name = $name\n";
print "Enter symbol: ";
chomp (my $ans_symbol = <STDIN>);
if (lc($ans_symbol) eq lc($symbol)) {
print "Correct. $symbol is the atomic symbol for $name\n";
} else {
print "Incorrect. $symbol is the atomic symbol for $name\n
+";
}
print "Enter $name\'s atomic number: ";
chomp (my $ans_number = <STDIN>);
if ($ans_number eq $number) {
print "Correct. The atomic number for $name is $number\n";
} else {
print "Incorrect. The atomic number for $name is $number\n
+";
}
print "\n";
}
}
sub number_test {
while (1) {
my $number = int(rand 109) + 1;
my $symbol = get_symbol($number);
my $name = get_name($number);
print "Atomic number = $number\n";
print "Enter symbol: ";
chomp (my $ans_symbol = <STDIN>);
if (lc($ans_symbol) eq lc($symbol)) {
print "Correct. $symbol has the atomic number $number\n";
} else {
print "Incorrect. $symbol has the atomic number $number\n"
+;
}
print "Which element has the atomic number $number: ";
chomp (my $ans_name = <STDIN>);
if (lc($ans_name) eq lc($name)) {
print "Correct. $number is the atomic number for $name\n";
} else {
print "Incorrect. $number is the atomic number for $name\n
+";
}
print "\n";
}
}
sub symbol_test {
while (1) {
my $number = int(rand 109) + 1;
my $symbol = get_symbol($number);
my $name = get_name($number);
print "Element symbol = $symbol\n";
print "Enter Name: ";
chomp (my $ans_name = <STDIN>);
if (lc($ans_name) eq lc($name)) {
print "Correct. $symbol is the symbol for $name\n";
} else {
print "Incorrect. $symbol is the symbol for $name\n";
}
print "Enter $symbol\'s atomic number: ";
chomp (my $ans_number = <STDIN>);
if ($ans_number eq $number) {
print "Correct. $symbol\'s atomic number is $number\n";
} else {
print "Incorrect. $symbol\'s atomic number is $number\n";
}
print "\n";
}
}
sub error {
my $error = shift;
print "Error: $error\n";
exit;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|