Blob


1 #!/usr/bin/perl
3 # Make a program that will repeatedly ask the user to guess a
4 # secret number from 1 to 100 until the user guesses the secret
5 # number. Your program should pick the number at random by using
6 # the magical formula int(1 + rand 100). See what the perlfun
7 # documentation says about int and rand if you're curious about
8 # these functions. When the user guesses wrong, the program should
9 # respond, "Too high", or "Too low." If the user enters the word
10 # quit or exit, or if the user enters a blank line, the program
11 # should quit. Of course, if the user guesses correctly, the
12 # program should quit then as well!
14 use warnings;
15 use strict;
16 use utf8;
18 my $secret = int(1 + rand 100);
19 my $guess;
20 print "Guess (1-100): ";
21 while (chomp($guess = <>) && $guess !~ /(exit|quit)|\A\z/) {
22 if ($guess == $secret) {
23 print "Correct!\n";
24 last;
25 } elsif (defined($guess) && $guess < $secret) {
26 print "Too low!\n";
27 } elsif (defined($guess) && $guess > $secret) {
28 print "Too high!\n";
29 }
30 print "Guess (1-100): ";
31 }
32 print "Exiting!\n";