我正在创建一个小程序,可以选择3种不同的语言,并输出Ruby,Python或数组中的随机元素.
但是我的if语句显然有一个语法错误,因为无论我尝试什么,我都会得到这个:
syntax error at test.pl line 15, near ") {"
syntax error at test.pl line 17, near "} elsif"
Execution of test.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)
这是我现在的代码:
sub welcome {
my @choices = qw( Perl Python Ruby );
my $lang = 3;
print("Welcome, to the test script, this will test what language you would like to learn.. In order to find out these choices, write this same definition in all three different languages\n");
print("There are", $lang, "languages to chose from please pick one:\n");
print "@choices";
my $choice = <STDIN>;
chomp $choice
if ($choice = "Ruby") {
print("You have chosen Ruby!\n");
} elsif ($choice = "Python") {
print("You have chosen Python!\n");
} else {
print("You're already writing in Perl!! Let me choose for you:");
my $rand_elm = @choices[rand @choices];
}
}
welcome();
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
my $choice = <STDIN>;
chomp $choice
if ($choice = "Ruby")
{
print("You have chosen Ruby!\n");
}
elsif ($choice = "Python")
{
print("You have chosen Python!\n");
}
else
{
print("You're already writing in Perl!! Let me choose for you:");
my $rand_elm = @choices[rand @choices];
}
}
Run Code Online (Sandbox Code Playgroud)
我也试过用strict;和warnings
我也尝试过 STDIN
所有这些都输出相同的错误.导致此错误的原因是什么?
您在以下情况后错过了分号:
chomp $choice
Run Code Online (Sandbox Code Playgroud)
请记住以下是有效的声明:
chomp $choice if ($choice = "Ruby")
Run Code Online (Sandbox Code Playgroud)
顺便说说,
$choice = "Ruby"
Run Code Online (Sandbox Code Playgroud)
应该
$choice eq "Ruby"
Run Code Online (Sandbox Code Playgroud)
=是标量赋值或列表赋值运算符.
==是数值比较运算符.
eq是字符串比较运算符.
| 归档时间: |
|
| 查看次数: |
58 次 |
| 最近记录: |