First off, always use strictures (use strict; use warnings;). If you had perl would have told you that there are only 8 planets (well, maybe not - it may have told you there are 767 known planets).
Rewriting your code using strictures, a while loop and making a couple of fixes (and cleaning out the comments) we get:
#!/usr/bin/perl use strict; use warnings; my $kSolarPlanets = 8; print "How many planets are there? "; while (defined(my $planets = <>)) { chomp $planets; if ($planets == $kSolarPlanets) { print "Well done there are indeed $kSolarPlanets planets in th +e solar system"; last; } print "Sorry that's not right, try again: "; }
Note the use of == for the numeric equality check instead of eq which performs a string equality check. 8 == 8.0 is true but 8 eq 8.0 is false.
Please don't clutter your code with comments that describe the exciting new language features you have just learned. They don't help the compiler and they don't help you. They do make it almost impossible to actually see the code!
Don't put multiple statements on one line (I'm looking at you else line) because the statements after the first tend to disappear.
Actually the real reason to use strictures is to pick up a range of trivial to make errors (like typos) that can lead to really hard to find bugs. Strict won't actually check that your planets constant is the correct number :(.
In reply to Re: Help using a while loop with STDIN
by GrandFather
in thread Help using a while loop with STDIN
by amonline
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |