Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Guess Number

by alandev (Scribe)
on Sep 01, 2006 at 18:36 UTC ( [id://570811]=CUFP: print w/replies, xml ) Need Help??

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Guess Number
by GrandFather (Saint) on Sep 01, 2006 at 19:21 UTC

    It is good practice to always include use strict; use warnings; at the start of your Perl code. They catch many types of coding errors early which make the errors easier to fix.

    White space and indentation help make code easier to read by breaking it up into functional units - much like using spaces, sentences and paragraphs in prose.

    Disallowing the user an easy way of exiting a program such as this is not very friendly.

    substr(rand(1)*10,0,1) is better done int rand 10.

    You chop the input for the first value, but not subsequent values. chomp is generally better used than chop in any case.

    The block of code:

    print "Guess the Code:"; $st=time(); $ip=<>; chop($ip); while($seed != $ip) { $seed=substr(rand(1)*10,0,1); $seed++; $ip=<>; }

    can be restructured so that the test is made last in the loop allowing the prompt and input handling code to only be written once (see below).

    The if following the loop is redundant - you don't get there until the test has succeeded in the loop.

    Combinging those suggestions, adding a little white space and indentation, and renaming a couple of variables to make them consistent with usage, you get something like:

    use strict; use warnings; my $st = time (); { my $code = 1 + int rand 10; print "Guess the Code:"; my $guess = <>; chomp $guess; redo if $code != $guess; } my $et = time (); my $tt = $et - $st; print "\n Time taken to break the Code ..: $tt seconds\n";

    DWIM is Perl's answer to Gödel
      You've moved "Guess the Code" into the loop, which may or may not be a good thing. I'd prefer do { } while to bare-BLOCK redo. There's no real need to strip newlines off of numeric input if you aren't storing it anywhere.

      How about:

      use strict; use warnings; print "Guess the Code:"; 1 while <> != (1+int rand 10); print "\n Time taken to break the Code ..: ", time()-$^T, " seconds\n +";
      (use warnings is only included to show that "42\n" numifies to 42 with no warning; it really should be removed to prevent warnings on more seriously non-numeric input.)

        I considered do {} while, but that required declaring the variables outside the loop and, besides, I felt there was some virtue in showing OP the bare block trick.

        Using the comma operator:

        use strict; use warnings; 1 while (print "Guess the Code: "), <> != (1+int rand 10); print "\n Time taken to break the Code ..: ", time()-$^T, " seconds\n +";

        gets the prompt printed for each iteration.

        Using $^T is nice btw. Something I'd not have thought of - I'm still learning. :)


        DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://570811]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2025-06-20 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.