in reply to Bad Code?
Also, people have suggested that you turn on warnings, but they fail to mention that you should do some checking of $guess (to see whether it's a number) before comparing it to $secret_number - otherwise, you will get warnings.
#!/usr/bin/perl use strict; use warnings; my $secret = 1 + int rand 100; while (print "Your guess > ") { local $_ = <>; chomp; if (!/\S/ || /quit/i || /exit/i) { print "Too bad, you gave up.\n"; exit; } if (/\D/) { print "You didn't enter a non-negative integer\n"; next; } if ($_ == $secret) { print "You guessed the number correctly.\n"; exit; } my $answer = $_ < $secret ? "less" : "greater"; print "The number you entered is $answer than the secret number\n" +; } __END__
That's how I would do it.
Abigail
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Bad Code?
by qq (Hermit) on Aug 19, 2003 at 20:49 UTC |