简体   繁体   中英

Beginning Perl syntax error at

Thanks for the help I have found on this site by searching the web when looking for problems.

It seams that I am unable to figure out just what the problem with this code is. I was told that Perl was easier than Bash but now that I have tried it I am not sure that is true at all. Anyway...I have been over this code and searched but have not been able to discover just what is wrong with it. Part of the problem is that my old brain does not learn new things so easy as it once did. The code

#!/usr/bin/perl
use warnings;
use strict;

my $target = 12;
my $guess=0;
print "Guess my number!\n";
print "Enter your guess: ";
$guess = <STDIN>;
while ($guess != $target) {
  if ($target == $guess) {
    print "That's it! You guessed correctly!\n";
    exit;
  }
  elsif ($guess > $target) {
    print "Your number is bigger than my number\n";
    print "Try agin";
    print "Enter a lower guess: ";
    $guess = <STDIN>;
  }
  else ($guess < $target) {  # syntax error at , near "else ("
    print "Your number is less than my number\n";
    print "Try agin";
    print "Enter a higher guess: ";
    $guess = <STDIN>;
  }
}  # syntax error at , near "}"

I am getting errors at the lines marked above. Yet the code is identical to the code in the elsif just above it.

The syntax is else { , since there is no condition for else , whereas elsif does have a condition. The syntax is:

if (condition) {
    ...
}
elsif (condition) {
    ...
}
else {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM