nancylebov: blue moon (Default)
[personal profile] nancylebov
This is an improved version of the program on page 7 of Learning Perl. The text is funnier, there are newlines after the questions, and it responds if you get the secret word right instead of just ending the program.

The hard part was figuring out where to put the "If guess = $secretword" conditional. It seemed reasonable to have it as an alternative after while statement, but the computer didn't agree with me. For all I know, it might be workable that way if I had found another tweak for the parentheses.

"Guessing the teacher's password" is a handy phrase for non-cognitive teaching which I picked up from Overcoming Bias.



$secretword = "llama"; # the secret word
print "What is your name?\n";
$name = ;
chomp $name;
if ($name eq "Randal") {
print "Hello, Randal. It's you!\n";
} else {
print "Hello, $name\n "; #ordinary greeting
print "What is the secret word?\n";
$guess = ;
chomp $guess;
if ($guess = $secretword){
print "You got it! Teacher explodes\n";
}
while ($guess ne $secretword) {
print "Wrong. Guess the teacher's password. ";
$guess = ;
chomp $guess;
}


}

December 2025

S M T W T F S
 123456
78910111213
141516 17181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Mar. 17th, 2026 07:05 pm
Powered by Dreamwidth Studios