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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.