PERL - how to do a program that will repeatedly ask the user to guess a secret...

  • Thread starter Thread starter Chovez A
  • Start date Start date
C

Chovez A

Guest
...number from 1 to 100...? until the user guesses the secret number

but the program should pick the number at random by using the magical formula int(1 + rand 100) and when the user guesses wrong, the program should respond "Too high" or "Too low." if the user enters the word quit or exit, or if the user enters a blank line, the program should quit.

and if the user guesses correctly, the program should quit then as well

help, please.. i'll pick the best answer ;)
 
#!/usr/bin/perl

use lib '../lib';
use strict;
use warnings;
use Continuity;

my $server = new Continuity(
ip_session => 0,
cookie_session => 'sid',
);
$server->loop;

sub getNum {
my $request = shift;
$request->print( qq{
Enter Guess: <input name="num" id=num>
<script>document.getElementById('num').focus()</script>
</form>
</body>
</html>
});
$request = $request->next;
my $num = $request->param('num');
return $num;
}

sub main {
my $request = shift;

my $guess;
my $number = int(rand(100)) + 1;
my $tries = 0;
my $out = qq{
<html>
<head><title>The Guessing Game</title></head>
<body>
<form method=POST>
Hi! I'm thinking of a number from 1 to 100... can you guess it?<br>
};
do {
$tries++;
$request->print($out);
$guess = getNum($request);
$out .= "It is smaller than $guess.<br>\n" if $guess > $number;
$out .= "It is bigger than $guess.<br>\n" if $guess < $number;
} until ($guess == $number);
$request->print("You got it! My number was in fact $number.<br>\n");
$request->print("It took you $tries tries.<br>\n");
}

1;
 
Back
Top