Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: how to enable, disable strict

by elsiddik (Scribe)
on Apr 25, 2007 at 10:20 UTC ( [id://611957]=note: print w/replies, xml ) Need Help??


in reply to Re: how to enable, disable strict
in thread how to enable, disable strict

now i have the code completed -- but when a user guess the right number it keeps asking for the same guess again and again , my question is : how can i restart the program automaticaly with a new secret number but without quiting . here is the code
#!/usr/bin/perl -w use warnings; use strict; my $secret = int(1 + rand 100); while(1) { print "please enter a guess from 1 to 100: "; chomp(my $guess = <STDIN>); if ($guess =~ /quit|exit|^s*$/i) { print " sorry you gave up.the number was $secret.\n"; last; } elsif ($guess < $secret) { no strict; print "you failed ,try higher\n"; } elsif ($guess > $secret) { print "you failed , try lower\n"; } elsif ($guess == $secret) { print "you got it\n"; } }
Sorry again for the mistakes i made concerning where should i add my question , im new here and new for PERL in general. cheers,

Replies are listed 'Best First'.
Re^3: how to enable, disable strict
by GrandFather (Saint) on Apr 25, 2007 at 10:37 UTC

    There are many ways to achieve that. An easy way is to repeat the code that generates of the number in the success block. Here's a slightly different way to do it using a "special" value (undef) and using an if as a statement modifier:

    #!/usr/bin/perl -w use warnings; use strict; my $secret; while (1) { $secret = int(1 + rand 100) if ! defined $secret; print "please enter a guess from 1 to 100: "; chomp (my $guess = <STDIN>); if ($guess =~ /quit|exit|^s*$/i) { print "Sorry you gave up. The number was $secret.\n"; last; } if ($guess < $secret) { print "You failed, try higher.\n"; } elsif ($guess > $secret) { print "You failed, try lower.\n"; } elsif ($guess == $secret) { print "You got it!\n"; $secret = undef; } }

    DWIM is Perl's answer to Gödel
      its working perfectly now ;) thx dude.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-24 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found