I am relatively new to perl, and I'm having a problem with loops. I'm trying to get a program for a simple number guessing game to repeat itself until you get the right number. Here is the code:

#!/usr/bin/perl -w

$thinkingof = int(rand 10);
print "Pick a number 0 to 10\n";
$guess = <STDIN>;
chomp $guess;

$count = 0;

while ($guess != $thinkingof && $count < 1){
	if ($guess > $thinkingof){
		print "You guessed too high.\n";
		}
	elsif ($guess < $thinkingof){
		print "You guessed too low.\n";
		}
	else {
		print "You guessed right.\n";
		}
	$count++;
	}

I have tried several different things, like (for the end)

while ($guess != $thinkingof){
	if ($guess > $thinkingof){
		print "You guessed too high.\n";
		}
	elsif ($guess < $thinkingof){
		print "You guessed too low.\n";
		}
	else {
		print "You guessed right.\n";
		}
	next if ($guess != $thinkingof)
	}

...and such, but nothings working. Usually, I get the "You guessed too high/low" message over and over. How do you prevent this from happening in a loop? Am I using the wrong kind of loop? Is there some sort of statement I don't know about? Any help will be appreciated.
By the way, if it's something that has to do with how you format it, and not a certain statement, please give me a hint; don't just tell me.
Thanks.

have you tried outputting the value of $thinkingof during each guess? This will help you to at least ensure that the randomness works.

You mean like this?

if ($guess < $thinkingof){
        print "You guessed higher than $thinkingof.\n";
}

I tried that, and the randomness is working; each time, it's a different number.

aha, maybe you should put the entire thing inside of the while loop ;)

I agree with Comatose, you should probably put the part that prompts for and accepts new input inside the loop somehow. That way as the loop is run again you ask for input again.

The psuedo-code looks something like:

  • While the user has not guessed the number
  • Prompt for a guess
  • Accept the guess
  • Compare the guess and report one of:
    • too low
    • too high
    • That's it

And as far as printing thinking of, I usually give it its own print statement right after it was generated with the word DEBUG in the output so it helps to remind me to take it out later. Something like print "DEBUG: thinkingof $thinkingof\n";

Your code is quite close to getting it right, here is a way to do this type of looping guessing game:

use strict;
use warnings;

my $thinkingof = int(rand 10)+1;
while (1) {
   print "Pick a number 1 to 10\n";
   my $guess = <STDIN>;
   chomp $guess;
   if ($guess > $thinkingof){
      print "You guessed too high\n";
      next;
   }
   elsif ($guess < $thinkingof){
      print "You guessed too low\n";
      next;
   }
   print "YOU GUESSED RIGHT!! It was $guess\n";
   last;
}

Thanks KevinADC, I just have two questions.
1) What is the +1 for after the int(rand 10)? I tried to see what happened if I got rid of it, and nothing was different.
2) Why is 1 the condition for the while loop? I tried what I had before, while ($guess != $thinkingof) and that told me I had an uninitialized value on that line. Is the 1 simply to get rid of that?

int(rand 10) returns numbers in the range 0 to 9
int (rand 10)+1 returns numbers in the range 1 to 10

while(1) is a continuous loop...it will keep looping until you 'break' or 'last'.

Thanks KevinADC, I just have two questions.
1) What is the +1 for after the int(rand 10)? I tried to see what happened if I got rid of it, and nothing was different.
2) Why is 1 the condition for the while loop? I tried what I had before, while ($guess != $thinkingof) and that told me I had an uninitialized value on that line. Is the 1 simply to get rid of that?

Murtan answered the questions but I want to expand a bit. Why the +1? Your code generates a random number 0-9. If the number is 0 (zero) then the person guessing the number can enter any non-numeric value and it will match because perl silently converts non-numeric values into 0 (zero) when used in numeric context, for example:

if ('hi' == 0) {
    print "WTF? Why does this print? 'hi' does not equal zero!";
}

You could check that the user input is just a simple number using a regexp, but to avoid the problem entirely I add 1 (one) to the return value of rand(10) so now the range is 1-10. Now that the zero is eliminated there can be no false matching of a string to a number. If you continue with perl the concept of how perl treats strings and number differently depending on the operator you use will become very important.

while(1) is an old construct to create an endless loop. It simply means:

while(there is a true value) {
   evaluate these expressions until 
  some condition breaks out of the loop (last).
}

Regards,
Kevin

#!/usr/bin/perl -w

print"pick a number 1 - 100:/n";
$guess = <STDIN>;
chomp ($thinkingof = (rand 100));

while($guess==$thinkingof) {
       if ($guess>$thinkingof) {
            print "you guessed too high/n";
       }
};
while($guess==$thinkingof) {
       if ($guess>$thinkingof) {
            print "you guessed too low/n";
       }
};
else ($guess = $thinkingof) {
        print "you got it right/n";
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.