Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
"be consistent"
 
PerlMonks  

having trouble with simple math game

by mynameisG (Novice)
on Nov 26, 2010 at 02:17 UTC ( #873754=perlquestion: print w/ replies, xml ) Need Help??
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!"; }

Comment on having trouble with simple math game
Download Code
Re: having trouble with simple math game
by biohisham (Priest) on Nov 26, 2010 at 02:52 UTC
    It seems to be reading the input as blank before the user even enters the answe
    Your code is not giving the user a chance to enter the number that will be saved in $response1, then, your comparisons operators are those used for strings and not for numbers, and above all, you missed important warnings by not useing strict and warnings, here is your code retouched....
    #!/usr/local/bin/perl use strict; use warnings; print "Content-type: text/html\n\n"; use CGI qw(:standard); my $cornplanted=0; my $cornnotplanted=0; #@number1=("12" .. "25"); #$number1rand=$number1[rand @number1]; my $number1rand=int(rand(20)); my $number2=int(rand(12)); my $correctanswer=($number1rand + $number2); print "$correctanswer <br>"; my $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; chomp(my $response1 = <STDIN>); if ($response1 == $correctanswer) { $cornplanted=($cornplanted + 1); print "correct!"; } elsif ($response1 != $correctanswer) { $cornnotplanted=($cornnotplanted + 1); print "not correct!"; }
    An interesting question, what if you wanted your program to respond to a non-conforming user entry (i.e when a user enters a sentence rather than a number)? Scalar::Util has an answer for you :)



    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

      Even with those changes it seems not be working correctly, when I hit the submit button, it still doesn't update the 'crops planted' and 'crops not planted'

Re: having trouble with simple math game
by tod222 (Pilgrim) on Nov 26, 2010 at 03:16 UTC

    Always use strict and warnings:

    use strict;
    use warnings;
    

    They are your friends:

    > ./crop.pl Global symbol "$response1" requires explicit package name at ./crop.pl + line 41. Global symbol "$response1" requires explicit package name at ./crop.pl + line 48. Execution of ./crop.pl aborted due to compilation errors.

    To get even that far I had to add some "my" statements:

    #!/usr/bin/perl use strict; use warnings; print "Content-type: text/html\n\n"; use CGI qw(:standard); my $cornplanted=0; my $cornnotplanted=0; #@number1=("12" .. "25"); #$number1rand=$number1[rand @number1]; my $number1rand=int(rand(20)); my $number2=int(rand(12)); my $correctanswer=($number1rand + $number2); print "$correctanswer <br>"; my $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!"; }

    You'll do much better to read the section CREATING FILL-OUT FORMS in the CGI.pm documentation and generate your form fields following proper CGI.pm practice.

    To debug your script, follow the recommendations in the DEBUGGING section of the CGI.pm docs.

      Okay it's getting really close, i got the code to update the 'crops planted' and the 'crops not planted'... but for some reason even though i am creating fill-out forms in the CGI.pm documentation, it is still reading the answer for the math problem ($num1 + $num2) before i get a chance to put in the input...so basically i type in a number and until the addition equals that number there is no output, any advice? thanks, and here's the updated code

      #!/usr/bin/perl use CGI qw(:standard); $num1=int(rand(20)); $num2=int(rand(12)); print header; print start_html('A Simple Example'), h1('A Simple Example'), start_form, "What's your name? ",textfield('name'), p, "What is $num1 + $num2? ", textfield('addition'), p, "What's the combination?", p, checkbox_group(-name=>'words', -values=>['eenie','meenie','minie','moe'], -defaults=>['eenie','minie']), p, "What's your favorite color? ", popup_menu(-name=>'color', -values=>['red','green','blue','chartreuse']), p, submit, end_form, hr; $correctanswer=($num1 + $num2); $cropsplanted=0; $cropsnotplanted=0; if ((param() and param('addition') == "$correctanswer")) { $cropsplanted=($cropsplanted + 1); $cropsnotplanted=($cropsnotplanted + 0); print "Correct answer: $correctanswer", p, "Crops planted: $cropsplanted", p, "Crops not planted: $cropsnotplanted", p, "Your name is",em(param('name')), p, "Your answer to the math problem is ", em(param('addition')), p, "The keywords are: ",em(join(", ",param('words'))), p, "Your favorite color is ",em(param('color')), } if ((param() and param('addition') != "$correctanswer")) { $cropsplanted=($cropsplanted + 0); $cropsnotplanted=($cropsnotplanted + 1); print "Correct answer: $correctanswer", p, "Crops planted: $cropsplanted", p, "Crops not planted: $cropsnotplanted", p, "Your name is",em(param('name')), p, "Your answer to the math problem is ", em(param('addition')), p, "The keywords are: ",em(join(", ",param('words'))), p, "Your favorite color is ",em(param('color')), }

        I figured out what is wrong, but I am not exactly sure how to fix. So I changed the random numbers so that they are no longer random... $num1=20; and $num2=12; and with that it was able to read the input... the problem is with using a random number each time i press submit the random number changes, therefore the answer changes. I need to figure out to store the random values ($num1 and $num2) into another scalar variable (i.e. $a and $b) so that the random numbers don't change when I hit submit. I can't figure out how to do it though? any help would be nice. and just for an fyi, you can't put

        $num1 =$a; $num2=$b;

        because $a and $b will equal random numbers. And I also tried

        my $a=$num1; my $b=$num2;
        and that still didn't work, any help would be great, thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://873754]
Approved by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-04-23 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (541 votes), past polls