Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: Guess Number by GrandFather
in thread Guess Number by alandev

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-24 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found