randomhero1270 has asked for the wisdom of the Perl Monks concerning the following question:
Hello! I am making a guessing game where the program guesses a number one through ten and the user inputs "high", "low", or "right". If the input is "high" the program guesses a lower number. If the input is "low" the program guesses a higher number. If the input is "right" the program prints a message and how many tries it took. I can get it to guess numbers but it doesn't guess lower if I type "high" and it doesn't guess higher if I type "low". My code is below.
use strict; my ($number, $new_guess, $my_hint, @guesses, $count); my $range = 9; $number = int(rand($range+1)); #min of 1 and high of 10, range does 0- +given int so i made the range 9 (0-9) and added one to it -> (1-10) print "My guess is: $number \n"; print "High, low, or right?\n"; while($my_hint ne "right"){ chomp($my_hint = <STDIN>); @guesses[$count] = $my_hint; if ($my_hint eq "low"){ $new_guess = int(rand($number)); print "I guess: " . $new_guess . "\n"; print "High, low, or right?\n"; } elsif ($my_hint eq "high"){ $new_guess = int(rand($number)); print "I guess: " . $new_guess . "\n"; print "High, low, or right?\n"; } elsif ($my_hint eq "right"){ print "It took me " . scalar @guesses . " tries\n"; } else{ print "What? Am I high, low, or right?\n"; } foreach $number (@guesses){ $count++; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Number Guessing Game
by tobyink (Canon) on Sep 23, 2012 at 20:19 UTC | |
Re: Number Guessing Game
by flexvault (Monsignor) on Sep 23, 2012 at 17:30 UTC | |
by randomhero1270 (Novice) on Sep 23, 2012 at 17:36 UTC | |
by roboticus (Chancellor) on Sep 23, 2012 at 17:47 UTC | |
by randomhero1270 (Novice) on Sep 23, 2012 at 18:17 UTC | |
by randomhero1270 (Novice) on Sep 23, 2012 at 17:57 UTC | |
by roboticus (Chancellor) on Sep 23, 2012 at 19:24 UTC | |
Re: Number Guessing Game
by Shaveta_Chawla (Sexton) on Sep 24, 2012 at 07:11 UTC | |
by nemesdani (Friar) on Sep 24, 2012 at 07:44 UTC |