Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with a program written in perl
#1
The following is a program written in perl about guessing a number. I need to add a win or loss reporting feature.

#!/usr/bin/perl
# guessnum.plx
use warnings;
use strict;
my $target = 12;
print "Guess my number!\n";
print "Enter your guess: ";
my $guess = <STDIN>;
if ($target == $guess) {
print "That's it! You guessed correctly!\n";
exit;
}
if ($guess > $target) {
print "Your number is bigger than my number\n";
exit;
}
if ($guess < $target){
print "Your number is less than my number\n";
exit;
}
Reply
#2
Code:
#!/usr/bin/perl

use warnings;
use strict;

sub GameStart {
    my $target = int(rand(101));
    print "Guess my number from 0 - 100!\n";
    print "Enter your guess: ";
    
    my $tries = 0;
    my $game = 0;
    while (!$game) {
        my $guess = <STDIN>;
        if ($guess == $target) {
            print "That's it! You guessed correctly!\n";
            $game = 1;
        } elsif ($guess gt $target) {
            print "Your number is bigger than my number, try again:\n";
        } elsif ($guess lt $target) {
            print "Your number is less than my number, try again:\n";
        }
        $tries ++;
    }
    
    print "It took you $tries tries\n";
}

GameStart

here's what I came up with, anything you don't understand just ask... I think everything here is pretty basic to look at an analyze, but in the event you don't see how something works by looking at it then just reply here.

Win/Loss is basically the same principle as the # of tries loop i've added in here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compile Error in Perl liveproject101 0 1,154 04-12-2013, 03:08 AM
Last Post: liveproject101
  [Perl] IRC Bot wchar_t 26 12,851 08-20-2012, 01:40 PM
Last Post: Trump
  Perl Ebook ven0m 2 1,623 05-04-2012, 08:14 AM
Last Post: ven0m
  HTML perl script AceInfinity 5 2,405 12-03-2011, 11:35 PM
Last Post: Closed Account
  Perl ARGV Tutorial AceInfinity 0 2,269 08-22-2011, 02:30 PM
Last Post: AceInfinity

Forum Jump:


Users browsing this thread: 1 Guest(s)