Just to provide a prompt for entering a number, ">" can be "%" or any other character for that matter, like you don't want the user to think that the scrip is hanging or something...
Now to return to line 4 like you want just use a label, a label is used in conjunction with goto, so the first segment of your code can be re-written as:
Notice, I did not use "chomp($userguess = <STDIN>);" in line 11 since the flow would go from 4 four onwards once again...until ($isover) { clear_the_screen(); print "Find the secret number between 1 and 100: \n\n"; LABEL: print "Enter your guess: \n\n % "; #">" #in book's scrip +t; why? chomp($userguess = <STDIN>); #following "while" loop added by me in a "challenge" while ($userguess !~ /^[+-]?\d+$/ ) { #testing to see whether user + entry is a number print "\nYour guess must be a number. Try again. \n\n"; goto LABEL; }
I'd stress on using the pragmas:
these can be very helpful at ensuring that you don't mistype a variable name and can give you helpful warnings and thus greatly reduce the debugging time..use strict; use warnings;
with $totalguesses, you can't return outside a subroutine, eval statement or do{}, hence using a print statement instead...
("if" loop beginning on line 17, pressing <Enter> after the 3 "elsif" choices returns the code to line 4)
Not entirely correct, what returns the code to line 4 is that the $isover in until(){} is not defined yet and hence this observation...
There's an extensive repetitive writing of certain statements in your code, that warrants taking a look at Perl subroutines which can enhance the logic-flow interpretation of the code..
Update: I did not mention that I pre-assigned $secretnumber with the value of 100....Finally, checking the Perl Documentation at the website or through your console is so rewarding so make it your new-year's new habit :).... Best of luck :)use strict; use warnings; my $isover; #using strict requires variables declaration wit +h 'my'. my $totalguesses; my $reply; until ($isover) { clear_the_screen(); print "Find the secret number between 1 and 100: \n\n"; LABELED: print "Enter your guess: \n\n % "; #labeling a posi +tion my $userguess; chomp($userguess = <STDIN>); while ($userguess !~ /^[+-]?\d+$/ ) { print "\nYour guess must be a number. Try again. \n\n"; goto LABELED; } $totalguesses++; my $secretnumber=100; if ($userguess == $secretnumber) { clear_the_screen(); print "You guessed it! Press <Enter> to return to the Main Me +nu.\n\n"; chomp($reply = <STDIN>); $isover = 1; #Defines $isover hence the evaluation would retu +rn true #the until(){} block would exit then... }elsif ($userguess > 100) { print "\nYou chose a number higher than 100.<Enter> to try aga +in.\n"; chomp($reply = <STDIN>); }elsif ($userguess < $secretnumber) { clear_the_screen(); print "$userguess is too low. Press <Enter> to guess again.\n\ +n"; chomp($reply = <STDIN>); }elsif ($userguess > $secretnumber) { clear_the_screen(); print "$userguess is too high. Press <Enter> to guess again.\n +\n"; chomp($reply = <STDIN>); } } print "your guesses $totalguesses\n"; sub clear_the_screen{ }
In reply to Re: Modifying loop structure and placement
by biohisham
in thread Modifying loop structure and placement
by irvson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |