Support Forums

Full Version: Help with a program written in perl
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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;
}
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.