My question is about code structure. Below I've printed some of the code that raises my question. I hope my #comments within the code will clarify what I'm doing. But just in case: 1) Ask the question 2) Check whether answer is correct or incorrect 3) Implement variables 4) Check to see if any inccorect answers 5) If "yes" to 4), ask student if s/he wants to see correct answer 6) If yes to 5), use the appropriate $question value to show the correct answer. I'm leaving out a lot of code of course. The full script actually runs! (No one more amazed than moi.) My question is whether my approach to presenting the answers to the questions answered incorrectly is the best approach. Is there another way to do this? Is there a better way to do this? An approach with less code?

#variable initialization @question = qw(1 2 3 4 5); $wrong_count = 0; . . . #sample question print "Question #1: Who is buried in Grant's tomb?\n\n"; print "A. Grant\n"; print "B. Lincoln\n"; print "C. Frank\n"; print "D. None of the above.\n\n\n\n"; print "The answer is: "; . . . #checking for correct answer if (lc ($reply) eq "a") { $answers[0] = "correct"; #creating @answers array } else { #the $incorrect variable doesn't figure into #the code in ques +tion here $answers[0] = "incorrect"; #following line added to gather info as which #answers are +incorrect with @question array $question[0] = "no"; } . . . #incrementing $wrong_count if ($question[0] eq "no") { print "Your answer to question number one is incorrect.\n\n"; $wrong_count++; } if ($question[1] eq "no") { print "Your answer to question number two is incorrect.\n\n"; $wrong_count++; } . . . if ($wrong_count != 0) { print "Would you like to see the correct answers to the questions +you missed?\n\n"; } . . . while ($wrong_count != 0) { if (lc $question[0] eq "no") { clear_the_screen(); print "Question #1: Who is buried in Grant's tomb?\n\n"; print "A. Grant\n"; print "B. Lincoln\n"; print "C. Frank\n"; print "D. None of the above.\n\n\n\n"; print "The answer is: "; print "The answer is: a\n "; ($wrong_count--);

In reply to Best code approach for this goal by irvson

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, details, 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, summary, 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.