http://www.perlmonks.org?node_id=873754

mynameisG has asked for the wisdom of the Perl Monks concerning the following question:

Hi, so i am trying to create a simple math game using perl. In the game there are 2 random numbers generated. Then the user has to do the math and type in the answer. If they get it right they plant a crop, if not the plant dies. I cannot get it to update the 'crop planted' or 'crop not planted'. It is like a point system. It starts a 0, then everytime the user gets the correct answer 'crop planted' goes up 1, but if they get it wrong 'crop not planted' goes up 1. It seems to be reading the input as blank before the user even enters the answer, but I'm not sure how to correct it. Here's the code...

#!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI qw(:standard); my $cornplanted=0; my $cornnotplanted=0; #@number1=("12" .. "25"); #$number1rand=$number1[rand @number1]; $number1rand=int(rand(20)); $number2=int(rand(12)); $correctanswer=($number1rand + $number2); print "$correctanswer <br>"; $output=<<_html_; <html> <body> <form method="post" action="foodaddition.cgi" name="form2"> Crops planted: $cornplanted <br> <input type="hidden" name="$cornplanted" id="$cornplanted" /> Crops not planted: $cornnotplanted <br> <input type="hidden" name="$cornnotplanted" id="$cornnotplanted" /> What does this math problem equal? <br> $number1rand + $number2 = <input type="text" name="response1" id="resp +onse1" /> <input type="hidden" name="response1" id="response1" /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> _html_ print $output; if ($response1 eq "$correctanswer") { $cornplanted=($cornplanted + 1); print "correct!"; } elsif ($response1 ne "$correctanswer") { $cornnotplanted=($cornnotplanted + 1); print "not correct!"; }