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++; } }
In reply to Chomping numbers? by 7cardcha
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |