#!/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; }

In reply to Chemistry Elements Quiz by cjf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.