As GrandFather said in the CB, you need while, next and last.
You should also add use strict; and use warnings; to the top of all your scripts. Without going into any great detail here, these will basically let you know if you've done something wrong.
You also need to know that Perl uses different operators for comparing strings and numbers (see perlop for details). Note how I put the 9 in quotes so that the ne operator is comparing a string with a string (you were comparing a string with a number).
Finally, as a matter of style, you've used far too many comments. Your code seemed to get rather lost amongst them and, in this instance, I don't see any that actually add anything helpful.
Putting all that together, you might end up with something like:
use strict; use warnings; my $planet_count = "9"; while (1) { print "How many planets are there? "; chomp(my $planet_guess = <>); if ($planet_guess ne $planet_count) { print "Sorry that's not right, try again.\n"; next; } else { print "Well done! There are indeed $planet_count planets in th +e solar system.\n"; last; } }
Example output:
How many planets are there? 3 Sorry that's not right, try again. How many planets are there? 6 Sorry that's not right, try again. How many planets are there? 9 Well done! There are indeed 9 planets in the solar system.
P.S. I think Pluto got demoted to planetoid (or something), so there's only 8 planets now. :-)
-- Ken
In reply to Re: Help using a while loop with STDIN
by kcott
in thread Help using a while loop with STDIN
by amonline
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |