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


}

Date: 2008-10-06 05:30 pm (UTC)
ext_58972: Mad! (Default)
From: [identity profile] autopope.livejournal.com
When sticking perl into a web page, you need to globally replace less-than and greater-than signs with their HTML element equivalents, i.e. replace < with &lt; and > with &gt;.

You also probably want to stick it in a block that preserves formatting.

Date: 2008-10-06 05:33 pm (UTC)
ext_58972: Mad! (Default)
From: [identity profile] autopope.livejournal.com
Errata: $guess = $secretword is wrong. The equals sign '=' assigns the contents of $secretword to $guess, it's not a test for equality. To test for equality where the variables are numbers, use == (two equal signs in succession); to test if strings are identical use 'eq' (for equals).

($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!
Edited Date: 2008-10-06 05:36 pm (UTC)

Date: 2008-10-06 06:45 pm (UTC)
From: [identity profile] nancylebov.livejournal.com
Thank you very much.

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

    
    }

Date: 2008-10-06 07:18 pm (UTC)
From: [identity profile] nancylebov.livejournal.com
Must check things....


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

    
    }

Date: 2008-10-07 02:54 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Couple of algorithmic problems.

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?

Date: 2008-10-07 05:18 pm (UTC)
From: [identity profile] nancylebov.livejournal.com
Thanks. That looks philosophically promising. Name things and move them around intentionally instead of hoping that they'll fall through the conditionals into the right places.

Date: 2008-10-07 05:25 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Pretty much. The reason we define the guess to be something definitely wrong right at the start is so that, the first time we compare, we *know* that it exists and we *know* it's wrong. If we don't define it, we have no idea what we're comparing to. It might be wrong. It might be *right*. It might be NULL POINTER EXCEPTION IN THREAD MAIN(). No matter what, it's not good.

(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")

Date: 2008-10-06 07:24 pm (UTC)
ext_90666: (Krosp thinking)
From: [identity profile] kgbooklog.livejournal.com
I don't know Perl, but it looks to me that if the user gives their name as "Randal", then the value of "$guess" will be tested before anything has been assigned to it. IOW, try moving the question and answer outside the else block.

Oh, and if you're going to replace < with the HTML version, you also should replace > ("gt").

Date: 2008-10-06 07:29 pm (UTC)
ext_90666: (Default)
From: [identity profile] kgbooklog.livejournal.com
Oh wait, is Randal not supposed to guess? (I was confused by the poor indenting.) Also, it would be better to delete the second if, and put the explosion after the while loop (right now it doesn't do anything if someone guesses right after a couple tries).

Date: 2008-10-06 08:30 pm (UTC)
From: [identity profile] nancylebov.livejournal.com
Yeah, Randal is the teacher's pet.

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. 18th, 2026 05:17 am
Powered by Dreamwidth Studios