irvson has asked for the wisdom of the Perl Monks concerning the following question:
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--);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Best code approach for this goal
by CountZero (Bishop) on Nov 23, 2009 at 22:59 UTC | |
|
Re: Best code approach for this goal
by almut (Canon) on Nov 23, 2009 at 22:53 UTC | |
|
Re: Best code approach for this goal
by GrandFather (Saint) on Nov 23, 2009 at 23:41 UTC | |
by irvson (Sexton) on Nov 24, 2009 at 22:09 UTC |