mardi 3 mai 2016

multiplication code not working properly

i have this code where it gives you two random numbers the first time and you have to enter the result of multiplying the two numbers.

if you enter a wrong answer it tells you to guess again but keeps the same random numbers until you answer correctly

if you enter the correct answer it well tell you correct and then starts over with another pair of numbers

the code i provided changes the value of the two rand numbers even if i entered the wrong number. i would like to keep the values the same until the correct answer is entered.

here is my code its very unpolished but its all i could do.

<?php
$num1=rand(1, 9);
$num2=rand(1, 9);
$num3=$num1*$num2;
$num_to_guess = $num3;
echo $num1."x".$num2."= <br>";


if ($_POST['guess'] == $num_to_guess) 
        { // matches!
            $message = "Well done!";
        } 
        elseif ($_POST['guess'] > $num_to_guess) 
        {
            $message = $_POST['guess']." is too big! Try a smaller number.";
        } 
            elseif ($_POST['guess'] < $num_to_guess) 
            {
                $message = $_POST['guess']." is too small! Try a larger number.";
            } 
                else  
                { // some other condition
                    $message = "I am terribly confused.";
                }
?>
<!DOCTYPE html>
<html>
<body>
<h2><?php echo $message; ?></h2>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="answer" value="<?php echo $answer;?>">
<input type="hidden" name="expression" value="<?php echo $expression;?>">
What is the value of the following multiplication expression: <br><br>
<?php echo $expression; ?> <input type="text" name="guess"><br>
<input type="submit" value="Check">
</form>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire