Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Number Guess

by Pied (Monk)
on Jul 15, 2005 at 06:46 UTC ( [id://475122]=note: print w/replies, xml ) Need Help??


in reply to Number Guess

Here's how I rewrote your code, with some explanations. This is like to make your code look more perlish, though this is how *I* 'd see your code (with heavy comments).
Should I code it myself, it would be different again...

#!c:\perl\bin\perl.exe -w # -w means "warn me if I do anything that i may not want to do (ie: ma +ke a typo in a var name)" use strict; #Because God wants it my $guess; #declare it somewhere like here seems nice TOP: my $tries = 10; my $value = int(rand(200)); print "Pick a number between 0 and 200.\nYou have ",$tries--," guesses + left.\n"; #Do it in one line. #the $tries-- prints $tries and then, decrements it. while ($tries>0 and defined ($guess=<STDIN>)){ #since you reread $guess each time, why not write it just once? #then until became a while, so I inversed the $tries test too. #defined is used because the <STDIN> can be undefined... #put the $tries test first, so that it doesn't ask for input when yo +u're over next if $guess!~m/^\d+\n$/; #this might be a bit ugly right now #just learn this as "it is an integer followed by a carriage return" + #And then, read about regexps if($guess>$value){ print "You need to guess a lower number.\nGuesses left: ",$tries-- +,"\n"; next; # jump again to the while loop NOW #same hacks as above. } if($guess<$value){ print "You need to guess a higher number.\nGuesses left: ",$tries- +-,"\n"; next; #same hacks as above. } #beeing here means we didn't jumped before (the "next"'s were not ca +lled print "You guessed my number in ",(10-$tries)," guesses. Thanks for +playing.\n"; $tries=0; } print "Sorry, you ran out of guesses. My number was $value\n" if ($gu +ess != $value); #Since the test is a "simple" if (this) then {just that} #you can write just that if (this); #looks like English. Easy to read. print "Play Again? (y/n) \n"; chomp(my $again = <STDIN>); #chomp ($again); goto TOP if($again =~m/y/i);#I don't like goto, but that's your code. #=~ means check this var #m/foo/ means "does it contain the word foo" #i means case insensitive #once again, the if can be written easier #else not needed! print "Goodbye!";

Once again, this is how *I* would see *your* code
Read some nodes, read some tuts and docs, learn the idioms, and then you'll write your perlish perl code.

P!

Replies are listed 'Best First'.
Re^2: Number Guess
by cryptoquip (Novice) on Jul 15, 2005 at 21:11 UTC
    Thanks for the comments--this looks elegant and useful. I'll take your advice and do more reading. :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-03-29 05:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found