By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,253 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,253 IT Pros & Developers. It's quick & easy.

Help with Incrementing

P: 4
And here it is.
I'm just, JUST learning how to use perl, and how to do any sort of programming in general. I'm trying to self-teach and have read a book on C programming and wanted to try PERL. I'm really very green at this and don't want to give up, but its not always easy to move forward without help so here goes. I have this simple, simple program that I am trying to write based off of some other examples I have read. Unfortunately, in order to get it to run in five or less trys, I do not think I'm executing the incrementing correctly. Can anyon take a quick look, point, laugh, and maybe guide me toward the sun?
So much appreciated, thank you.

Expand|Select|Wrap|Line Numbers
  1. #!/user/bin/perl
  2.  
  3. my $count = 0;
  4. my $num = int(rand (21));
  5. my $guess = <STDIN>;
  6.  
  7. do {
  8.   print "Guess a number from 1-20:"; 
  9.  
  10.   if($guess == $num) {
  11.     print "You win. Go start your 1-900 number 
  12.  
  13. immediately.\n";
  14. }
  15.   elsif( $guess > 20) {
  16.     print "Uhh, lets try that again. A number, 
  17.  
  18. one through 20. Go:\n";
  19. }
  20.   elsif( $guess < 1) {
  21.     print "No dice. 1-20. Try again.\n";
  22. }
  23. $count++;
  24.  
  25.  
  26. while ($guess!= $num) {
  27.  if ($count <=4) {
  28.    print "Nope. Guess again.\n";
  29. }
  30.   else ($count == 5) {
  31.     print "You lose. Don't quit your day job.\n" || die;
  32. }
Feb 28 '08 #1
Share this Question
Share on Google+
8 Replies


eWish
Expert 100+
P: 971
We like it when people are trying to learn. Therefore, we are not going to laugh at you. Try removing the while loop and see if that does what you want.

Change:
Expand|Select|Wrap|Line Numbers
  1. while ($guess!= $num) {
  2.  if ($count <=4) {
  3.    print "Nope. Guess again.\n";
  4. }
  5.   else ($count == 5) {
  6.     print "You lose. Don't quit your day job.\n" || die;
  7. }
  8.  
To:
Expand|Select|Wrap|Line Numbers
  1. if ($count <=4) {
  2.    print "Nope. Guess again.\n";
  3. }
  4.   else ($count == 5) {
  5.     print "You lose. Don't quit your day job.\n" || die };
Also, look into using the following pragmas. They will save you time debugging and help you write cleaner & stricter code.

strict - Perl pragma to restrict unsafe constructs
warnings - Perl pragma to control optional warnings

--Kevin
Feb 29 '08 #2

P: 4
Thank you so much for your help. I have tried something simaliar to that... and even replacing it with the exact code you provided, I get the following error in the command prompt window:

Syntax error at... line 26 near "else ("
Missing right curly or square bracket at line 28, at end of line

Then it just says execution aborted due to compilation errors.

I think overall, it feels like a messy program with so many if statements, but like I said, I'm just starting. Here again is the code that I have including the revisions you suggested.


Expand|Select|Wrap|Line Numbers
  1. #!/user/bin/perl
  2.  
  3. my $count = 0;
  4. my $num = int(rand (21));
  5. my $guess = <STDIN>;
  6.  
  7. do {
  8.   print "Guess a number from 1-20:"; 
  9.  
  10.   if($guess == $num) {
  11.     print "You win. Go start your 1-900 number 
  12.  
  13. immediately.\n";
  14. }
  15.   elsif( $guess > 20) {
  16.     print "Uhh, lets try that again. A number, 
  17.  
  18. one through 20. Go:\n";
  19. }
  20.   elsif( $guess < 1) {
  21.     print "No dice. 1-20. Try again.\n";
  22. $count++;
  23.  
  24. }
  25.  
  26.  
  27. if ($count <=4) {
  28.   print "Nope. Guess again. \n";
  29. }
  30. else ($count == 5) {
  31.   print "You lose. Don't quit your day job.\n" || 
  32.  
  33. die};
----- Thanks again!
Feb 29 '08 #3

eWish
Expert 100+
P: 971
When posting code here please use the [CODE=perl][/CODE] tags.

The you have forgot the closing bracket on the do{} block of code.

--Kevin
Feb 29 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
The first thing that jumps out is this:

Expand|Select|Wrap|Line Numbers
  1.  else ($count == 5) {
an "else" block does not have a conditional expression (xxxxx). This error should prevent your program from running, the correct syntax is:

Expand|Select|Wrap|Line Numbers
  1. if (condition) {
  2.    expression
  3. }
  4. elsif (condition) { # optional elsif block, there can be more
  5.    expression
  6. {
  7. else {# optional else block but there can only be one
  8.    fall through condition
  9. }
"elsif" and "else" are both optional after an "if" block. But "else" has no condition like "if" and "elsif".

There might be other problems with your code too, I will check later.
Feb 29 '08 #5

P: 4
Thank you so much. I think my problems run much deeper than I thought.
Feb 29 '08 #6

mehj123
P: 55
Hi....
The do loop you are using should have a condition to determine where the loop should stop its execution. And
Expand|Select|Wrap|Line Numbers
  1.  $guess = <STDIN>; 
should be inside the loop only.. One more change that is required is for each time the user fails to guess the number correctly you should increase the $count... Moreover, as mentioned by KevinADC, else construct does not have any conditions .With these changes the code is given below.. it would be needed to be refined furthur.. :)

Expand|Select|Wrap|Line Numbers
  1. #/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $count = 0;
  5. my $num = int(rand (21));
  6. my $guess = 0;
  7.  
  8. do
  9. {
  10.     print "Guess a number from 1-20:";
  11.     $guess = <STDIN>;
  12.     if($guess == $num)
  13.     {
  14.         print "You win. Go start your 1-900 number immediately.\n";
  15.         exit(0);
  16.     }
  17.     elsif( $guess > 20)
  18.     {
  19.         print "Uhh, lets try that again. A number,one through 20. Go:\n";
  20.     }
  21.     elsif( $guess < 1)
  22.     {
  23.         print "No dice. 1-20. Try again.\n";
  24.     }
  25.     if ($count <=4)
  26.     {
  27.         print "Nope. Guess again. \n";
  28.         $count++;
  29.     }
  30.     else
  31.     {
  32.         print "You lose. Don't quit your day job.\n";
  33.         exit(0);
  34.     }
  35. }while($count<=5);
Hope this helps..
Mehj
Feb 29 '08 #7

P: 4
very helpful, thank you. I see just what you are saying!
Feb 29 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
May as well clean it all up:


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $count = 5;
  5. my $num = int(rand (21));
  6. print "Guess a number from 1-20:";
  7. chomp(my $guess = <STDIN>);
  8.  
  9. do {
  10.     if( $guess > 20 || $guess < 1) {
  11.         print "Uhh, lets try that again. Enter a number, 1 through 20:";
  12.         chomp($guess = <STDIN>);
  13.     }
  14.     elsif (--$count == 0) {
  15.         print "You lose. The number was $num.\n";
  16.     }
  17.     elsif( $guess != $num) {
  18.         print "Wrong guess. $count more tries left. Enter a number, 1 through 20:";
  19.         chomp($guess = <STDIN>);
  20.     }
  21.     else {
  22.         print "You win. $num is correct! Go start your 1-900 number immediately.\n";
  23.         $count=0;
  24.     }       
  25. } while( $count );
Feb 29 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.