Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Guess the number

by chip_creep (Acolyte)
on Dec 22, 2003 at 17:10 UTC ( #316415=perlcraft: print w/ replies, xml ) Need Help??

   1: #   Howdy! This is my very first Perl script! I know it's kinda short
   2: #   but any response would be great. What I am trying to do next is to make 
   3: #   it in a CGI/HTML format to put on a web page. Hints anyone?
   4: 
   5: 
   6: 
   7: #!/usr/local/bin/perl -w
   8: 
   9: print "Guess the number.";
  10: print "DIRECTIONS:\nGuess a number between 1 and 100 inclusive.\n\n";
  11: $random = int(rand(100)+1);      # I added 1 to the random
  12:                                  # number generator to push the
  13:                                  # possible numbers from 0-99 to 1-100
  14: 
  15: for ($try=6;$try>0;$try--){      # $try is the number of tries you get (DUH!)
  16:     print "You have 1 try left. Use it wisely!\n\n" if($try ==1);
  17:     print "You have $try tries left.\n\n" if($try !=1);
  18:     print "What is your guess? -->";
  19:     do {
  20:         $guess = <STDIN>; chomp $guess;
  21: 
  22:      }while (!$guess);
  23:     print "HA! HA! too HIGH.\n" if ($guess>$random);
  24:     print "OH! NO! too LOW.\n" if ($guess<$random);
  25:     if ($guess==$random){
  26:         print "'Bout time you guessed it.   Stupid head.\n\n" ;
  27:         exit;
  28:     }
  29: }
  30: print "The answer was $random. And so is your IQ.\n\n\nSTUPID!!\n\n";
  31: exit;
  32: 
  33: 
  34: ############################################################
  35: #           UPDATED --- 1/11/03                            #
  36: #  NOTE: the problem with this is that it loops            #
  37: #  printing the button text box ect. w/o giving you a      #
  38: #  chance to guess. Still a work in progress.              #
  39: #  Any generous monk out there willing to give me a        #
  40: #  little nudge?                                           #
  41: ############################################################
  42: 
  43: 
  44: 
  45: #!/usr/local/bin/perl -w
  46: 
  47: print "Content-type: text/html\n\n";
  48: use CGI;
  49: $query = new CGI;
  50: print "<HTML><HEAD><title>Guess the number.</title></HEAD><BODY>";
  51: print "<H1>DIRECTIONS:\nGuess a number between 1 and 100 inclusive.\n\n</H1>";
  52: $random = int(rand(100)+1);
  53: 
  54: for ($try=6;$try>0;$try--){
  55:     print "You have 1 try left. Use it wisely!\n\n" if($try ==1);
  56:     print "You have $try tries left.\n\n" if($try !=1);
  57:     print "What is your guess? -->";
  58:     print '<INPUT TYPE="text" NAME="guess">';
  59:     print '<INPUT TYPE = "submit">';
  60:     print "HA! HA! too HIGH.\n" if ($guess>$random);
  61:     print "OH! NO! too LOW.\n" if ($guess<$random);
  62:     if ($guess==$random){
  63:         print "'Bout time you guessed it.   Stupid head.\n\n" ;
  64:         print"</BODY>";
  65:         exit;
  66: #         print "The answer was $random. And so is your IQ.\n\n\nSTUPID!!\n\n";exit;
  67:     }
  68: }
  69: print "The answer was $random. And so is your IQ.\n\n\nSTUPID!!\n\n";
  70: print"</BODY>";
  71: exit;

Comment on Guess the number
Download Code
•Re: Guess the number
by merlyn (Sage) on Dec 22, 2003 at 17:39 UTC
Re: Guess the number
by mkirank (Chaplain) on Jan 08, 2004 at 04:34 UTC
    do the following steps
    1.put this file in a web server directory which executes cgi scripts and change permission to 755 (assuming a *nix server)
    2.add print " Content-type: text/html \n\n" at top of the program
    3.create a html form with a input box and a submit button
    4. u can pass the num of tries either from a hidden variable from the HTML form or set a cookie
    5. use CGI.pm and get the variables instead of reading from STDIN
    hope this helps
      Hi I'm back. While I haven't tried your post out yet, I did get a book on cgi so that should help me out as well. Will post again when I get some thing reasonable to work.

      Original post updated!!!!

      Hello. This is my signature.
        Hey is your computer science companion. It's not waiting for imput and when you try to print $guess before the exit it does nothing, so guess doesn't ever get a value... I'll clean it up a bit and send it back...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2014-09-17 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (95 votes), past polls