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++; } }
In reply to Number Guessing Game by randomhero1270
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |