A perl program
Oct. 6th, 2008 12:28 pmThis 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;
}
}
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;
}
}
no subject
Date: 2008-10-06 05:30 pm (UTC)You also probably want to stick it in a block that preserves formatting.
no subject
Date: 2008-10-06 05:33 pm (UTC)($guess = $secretword) assigns the value of $secretword to $guess, and the value of the expression (in brackets) is true, because the variable assignment worked, so this code always executes the following block (with the print statement).
PS: subtle logic bug aside, at least you got code that compiles! Good going, and keep it up!
no subject
Date: 2008-10-06 06:45 pm (UTC)As for that subtle logic error, aside from getting more familiar with the language, I need to think about adequate testing.
If you saw a ghost version or two, that was me finding out that disable formatting is the Wrong Thing, and <pre> is possibly the Right Thing. Also, taking a fast glance at ampersand l t ; and assuming that the l stands for 'left' wasn't quite it.
$secretword = "llama"; # the secret word print "What is your name?\n"; $name = <STDIN> 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 = <STDIN> chomp $guess; if ($guess eq $secretword){ print "You got it! Teacher explodes\n"; } while ($guess ne $secretword) { print "Wrong. Guess the teacher's password. "; $guess = <STDIN> chomp $guess; } }no subject
Date: 2008-10-06 07:18 pm (UTC)$secretword = "llama"; # the secret word print "What is your name?\n"; $name = <STDIN>; 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 = <STDIN>; chomp $guess; if ($guess eq $secretword){ print "You got it! Teacher explodes\n"; } while ($guess ne $secretword) { print "Wrong. Guess the teacher's password. "; $guess = <STDIN>; chomp $guess; } }no subject
Date: 2008-10-07 02:54 pm (UTC)First: What happens if you guess wrong the first time, then guess right, later? In your program, guessing riht the first time makes the teacher explode. Guessing wrong just breaks you out of your While loop and drops you out the bottom of the program.
Second: The use of the first "If" and then the while is a little redundant and harder to follow. A better algorithm might be this (in pseudocode, not Perl):
define secretword=llama
define guess=anincorrectguess
ask for name
- if name = Randal, respond and end.
- else continue
Request name.
while guess != secretword do:
{
- ask for guess
- if guess != secretword respond "wrong"
}
respond "correct!"
Basically, my program *opens* by defining an initial value of guess that is absolutely totally going to be wrong the first time the While loop runs, so that under every circumstance the While loop will run at least once. Inside the While loop, it prompts for a guess, and if the guess is wrong, tells you then loops again. If the guess is right, it says nothing, and drops you out of the loop. Once you've left the loop, you get the "you're right!" response. Because, after all, there's no way to get out of that loop without being right.
Does that make sense?
no subject
Date: 2008-10-07 05:18 pm (UTC)no subject
Date: 2008-10-07 05:25 pm (UTC)(Also: I asked for the name twice in my pseudocode, and I shouldn't have. Delete the "request name" and replace it with "Say hello to $name")
no subject
Date: 2008-10-06 07:24 pm (UTC)Oh, and if you're going to replace < with the HTML version, you also should replace > ("gt").
no subject
Date: 2008-10-06 07:29 pm (UTC)no subject
Date: 2008-10-06 08:30 pm (UTC)