in reply to Re: Logical Operators
in thread Logical Operators

This is actually to check the answers to a 25 questions quiz. If someone get all the answers correct, then a prize is given. Is there a better way to write the codes?

Replies are listed 'Best First'.
Re: Re: Re: Logical Operators
by myocom (Deacon) on May 26, 2001 at 10:19 UTC

    Certainly.

    Each time an answer is given, see if it's right, then increment a counter. At the end, you'll have the number of right answers. The code below reads very much like that sentence:

    # The code has already asked a question at this point # ("What is the answer to Life, the Universe, and Everything"). # The user has put in his/her answer. # The program has stored it in a scalar, $answer. if ($answer == 42) { $rightanswers++; } # Put that sort of logic in your question/answer loop, # then you can end with something like this: if ($rightanswers == 25) { print "You got all of the questions right! You win!\n"; } elsif ($rightanswers > 20) { print "You got almost all of the questions right. Not bad!\n"; } elsif ($rightanswers > 10) { print "You could've done better...\n"; } elsif ($rightanswers > 1) { print "You did quite poorly...\n"; } else { print "Were you even reading the questions...?\n"; }