6 Replies - 542 Views - Last Post: 02 October 2017 - 10:00 AM Rate Topic: -----

#1 williamscel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-October 08

Guessing a random number

Posted 01 October 2017 - 12:28 AM

Hello,
I am trying to write a php script that generates a random number between and including 1 and 1000. the user will guess this number and the attempts recorded. once the correct number is guessed a message will be displayed telling the user the number was correctly guessed and the number of attempts. I am getting 500 errors. can you please help me figure this out.

Much thanks.
<?php
//$num_to_guess = 42;
$min = 1;
$max = 1000;
$randomvalue = mt_rand($min,$max);
$attemp = 0;
if (!isset($_POST["randonvalue"])) {
	 $message = "Welcome to the guessing machine!";
	 while($randonvalue != "guess"){
} else if ($_POST["randonvalue"] > $randomvalue {
	$message = $_POST["guess"]." is too big! Try a smaller number.";
	    ++$attemp;
} else if ($_POST["randonvalue"] < $num_to_guess) {
	$message = $_POST["guess"]." is too small! Try a larger number.";
	    ++attemp;
     }
} else { // must be equivalent
	$message = "You guessed the right number in".$attemp "attemps.";
}
?>
<html>
<head>
<title>A PHP number guessing script</title>
</head>
<body>
<h1><?php echo $message; ?></h1>
<form action="" method="POST">
<p><strong>Type your guess here:</strong>
<input type="text" type="hidden" name="randonvalue"></p>
<p><input type="submit" value="submit your guess"/></p>
</form>
</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Guessing a random number

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: Guessing a random number

Posted 01 October 2017 - 12:59 AM

There is basic syntax error on line 10. If your editor/IDE doesn't indicate this then there are online syntax checkers you could use if needed.

e.g. https://phpcodechecker.com/
Was This Post Helpful? 0
  • +
  • -

#3 williamscel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-October 08

Re: Guessing a random number

Posted 01 October 2017 - 10:45 AM

View Postandrewsw, on 01 October 2017 - 01:59 AM, said:

There is basic syntax error on line 10. If your editor/IDE doesn't indicate this then there are online syntax checkers you could use if needed.

e.g. https://phpcodechecker.com/


I reworked the code and used the link above. im getting one error on line 10. im not sure how to fix this.
<?php
//$num_to_guess = 42;
$min = 1;
$max = 1000;
$randomvalue = mt_rand($min,$max);
$attemp = 0;
if (!isset($_POST["guess"])) {
	 $message = "Welcome to the guessing machine!";
	 while($randonvalue != "guess"){
} else if ($_POST["guess"] > $randomvalue){
	$message = $_POST["guess"]." is too big! Try a smaller number.";
	    ++$attemp;
} else if ($_POST["guess"] < $randomvalue) {
	$message = $_POST["guess"]." is too small! Try a larger number.";
	    ++attemp;
     }
} else { // must be equivalent
	$message = "You guessed the right number in".$attemp "attemps.";
}
?>
<html>
<head>
<title>A PHP number guessing script</title>
</head>
<body>
<h1><?php echo $message; ?></h1>
<form action="" method="POST">
<p><strong>Type your guess here:</strong>
<input type="text" type="hidden" name="randonvalue"></p>
<p><input type="submit" value="submit your guess"/></p>
</form>
</body>
</html>



Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: Guessing a random number

Posted 01 October 2017 - 11:24 AM

What error.
Was This Post Helpful? 0
  • +
  • -

#5 williamscel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-October 08

Re: Guessing a random number

Posted 01 October 2017 - 05:46 PM

the error is:
PHP Syntax Check: Parse error: syntax error, unexpected 'else' (T_ELSE) in your code on line 10
} else if ($_POST["guess"] > ($randomvalue)){
PHP Syntax Check: Errors parsing your code
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: Guessing a random number

Posted 01 October 2017 - 11:22 PM

Why are you attempting to open a while loop on line 9? That is the issue.
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1829
  • View blog
  • Posts: 5,760
  • Joined: 15-January 14

Re: Guessing a random number

Posted 02 October 2017 - 10:00 AM

The random while loop is one problem, and you have errors on lines 15 and 18 also. Also, the random number is going to change every time they submit the form, you're not going to generate one number which they keep trying to guess. Each time they submit the form you generate a new random number. Also, it's spelled "attempt".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1