Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:44:49+00:00 2026-06-15T05:44:49+00:00

I’m very new to Perl and have been given an assignment that is a

  • 0

I’m very new to Perl and have been given an assignment that is a simple guessing game where the user is given 8 chances to guess a number between 1 and 100. I keep getting the error mentioned above and cannot figure it out.

Here’s my code:

    use Modern::Perl;

    my ($guess,$target,$counter); 
    $target = (int rand 100) + 1;

    while ($guess < $target)
    {
        chomp ($guess=<>);
        print "Enter guess $counter: ";

        $counter++;

        if ($guess eq $target) {
            print "\nCongratulations! You guessed the secret number $target in   $counter";
        }
        elsif ($guess > $target) {
            print "\nYour guess, $guess, is too high.";
        }
        elsif ($guess < $target) {
            print "\nYour guess, $guess, is too low.";
        }
        else {
            print "You lose. The number was $target.";
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:44:50+00:00Added an answer on June 15, 2026 at 5:44 am

    Your code suffers a few issues. Here is my code, using a different approach:

    #!/usr/bin/perl
    
    use 5.012;   # use strict; use feature 'say';
    use warnings;
    
    my $number = (int rand 100) + 1;
    my $max_guesses = 8;
    
    GUESS: foreach my $guess_no (1..$max_guesses) {
      say "($guess_no) Please enter a guess:";
      my $guess = <>;
      chomp $guess;
    
      unless ($guess =~ /^\d+$/) {
        say "Hey, that didn't look like a number!";
        redo GUESS;
      }
    
      if ($guess == $number) {
        say "Congrats, you were on target!";
        last GUESS;
      } elsif ($guess < $number) {
        say "Nay, your guess was TOO SMALL.";
      } elsif ($guess > $number) {
        say "Nay, your guess was TOO BIG.";
      } else {
        die "Illegal state";
      }
    
      if ($guess_no == $max_guesses) {
        say "However, you have wasted all your guesses. YOU LOOSE.";
        last GUESS;
      }
    }
    

    Example usage:

    $ perl guess-the-number.pl
    (1) Please enter a guess:
    15
    Nay, your guess was TOO SMALL.
    (2) Please enter a guess:
    60
    Nay, your guess was TOO BIG.
    (3) Please enter a guess:
    45
    Nay, your guess was TOO BIG.
    (4) Please enter a guess:
    30
    Nay, your guess was TOO SMALL.
    (5) Please enter a guess:
    38
    Congrats, you were on target!
    

    (all other corner cases (too many guesses, non-numbers as input) work as expected)

    What did I do differently?

    • I didn’t loop while the guess was too small (← bug!). Instead, I did a loop iteration for each guess. However, a while (1) loop would have worked as well.
    • I did a sanity check on the input, using a simple regular expression. It asserts that the input will be considered numeric by Perl. Else, you get to redo the guess.
    • I initialize all variables as soon as I declare them. This removes any possibilities of uninitialized values popping up in error messages.
    • I use proper comparision operators. Perl scalars come in two flavours: stringy and numeric:

      Stringy  Numeric
      lt       <
      le       <=
      eq       ==
      ne       !=
      ge       >=
      gt       >
      cmp      <=>
      
    • The say function prints the string like print, but appends a newline. This removes akward \ns at the beginning or the end of the string. It makes reading code easier.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have been unable to fix a problem with Java Unicode and encoding. The
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.