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.