in reply to having trouble with simple math game
It seems to be reading the input as blank before the user even enters the answeYour 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....
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 :)#!/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!"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: having trouble with simple math game
by ysth (Canon) on Nov 26, 2010 at 03:02 UTC | |
|
Re^2: having trouble with simple math game
by mynameisG (Novice) on Nov 26, 2010 at 03:23 UTC |