nancylebov (nancylebov) wrote,
nancylebov
nancylebov

A perl program

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;
}


}
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 10 comments