7cardcha has asked for the wisdom of the Perl Monks concerning the following question:
So, obviously I need to chomp my $state variable, or it doesn't work. However, I don't need to chomp my number variables($top,$bottom). I'm guessing Perl automagically chomps them when doing mathematical operations? Should I chomp them manually for style? Also, anything also "wrong", or could be done better with my code? Thanks, wise Perl Monks for your time!use strict; use warnings; sub input { print($_[0]); return <STDIN>; } my $bottom = input("Enter the bottom most number: "); my $top = input("Enter the top most number: "); input("Hit enter once you have your number!"); my $middle = int(($bottom + $top) / 2); my $guesses = 1; while(1) { my $state = input("Is it " . $middle . "(l/h/t)?: "); chomp($state); if($state eq "t") { input("I Win! It took me " . $guesses . " guesses!"); last; } elsif($state eq "l") { $top = $middle; $middle = int(($top + $bottom) / 2); $guesses++; } elsif($state eq "h") { $bottom = $middle; $middle = int((($bottom + $top) / 2) + 0.5); $guesses++; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Chomping numbers?
by tobyink (Canon) on Jul 18, 2013 at 06:37 UTC | |
by 7cardcha (Novice) on Jul 18, 2013 at 14:23 UTC | |
|
Re: Chomping numbers?
by Anonymous Monk on Jul 18, 2013 at 03:28 UTC | |
by 7cardcha (Novice) on Jul 18, 2013 at 04:23 UTC | |
by davido (Cardinal) on Jul 18, 2013 at 04:48 UTC | |
by derby (Abbot) on Jul 18, 2013 at 11:53 UTC |