Perl 简单的正面或反面游戏

发布于 2025-01-10 05:01:22 字数 421 浏览 0 评论 0原文

我正在尝试用 Perl 制作一个简单的正面或反面游戏。但无论答案正确与否,它都告诉我我输了。我在这里错过了一些基本的东西吗?任何帮助将不胜感激。

print "Heads or tails?";
$guess = <>;
print "Your guess is ",$guess;
$randNum = int(rand(10));

if ($randNum % 2 ==0){
    $answer = "heads";
} else {
    $answer = "tails \n";
}

print "The right answer is ", $guess;
$c = $guess eq $answer;
if ($c == 1){
    print "you win!";
} else {
    print "you lose";
}

Im trying to make a simple heads or tails game in perl. But no matter if the answer is correct or not it tells me i lose. Am i just missing something fundamental here? Any help would be appreciated.

print "Heads or tails?";
$guess = <>;
print "Your guess is ",$guess;
$randNum = int(rand(10));

if ($randNum % 2 ==0){
    $answer = "heads";
} else {
    $answer = "tails \n";
}

print "The right answer is ", $guess;
$c = $guess eq $answer;
if ($c == 1){
    print "you win!";
} else {
    print "you lose";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2025-01-17 05:01:22
$c = $guess eq $answer;

这将考虑您答案中的确切字符串,并且您的答案有一半的时间有换行符,并且两次都是您的猜测。另外,您在 1 个答案后还有一个空格。

例如,假设您猜“正面”,那么您的 $guess 字符串将是:

$guess = "heads\n";   # newline added when reading with <>

您的答案将是以下其中之一

$answer = "heads";
$answer = "tails \n";

所以让我们比较这些...

# start    end
# |heads\n|
# |heads|
# |tails \n|

如您所见,唯一可能的方法eq 正确的情况是您猜到了 "tails ",其中包含空格。

您需要做的是清理您的输入,并且答案中不要有任何换行符。

例如:

chomp(my $guess = <>);    # remove newline from input
...
$answer = "heads";
...
$answer = "tails";

您还可以去除空格并使字符串大小写相同。

$guess =~ s/\s//g;      # strip whitespace
$guess = lc($guess);    # make lowercase

另外,请注意,在没有这两个编译指示的情况下编写 Perl 代码会使您的生活变得非常困难,因此请始终包含它们:

use strict;
use warnings;
$c = $guess eq $answer;

This will take into consideration the exact string in your answer, and your answer has a newline half the time, and your guess both times. Also you have a space after 1 answer.

For example, lets say you guess "heads", then your $guess string will be:

$guess = "heads\n";   # newline added when reading with <>

Your answer will be either of these

$answer = "heads";
$answer = "tails \n";

So lets compare those...

# start    end
# |heads\n|
# |heads|
# |tails \n|

As you can see, the only possible way for eq to be true is if you guessed "tails ", with an included space.

What you need to do is to clean up your input, and not have any newlines in your answer.

For example:

chomp(my $guess = <>);    # remove newline from input
...
$answer = "heads";
...
$answer = "tails";

You can also strip whitespace and make the string the same case.

$guess =~ s/\s//g;      # strip whitespace
$guess = lc($guess);    # make lowercase

Also, note that writing Perl code without these two pragmas make your life excessively hard, so always include them:

use strict;
use warnings;
司马昭之心 2025-01-17 05:01:22

当您阅读答案时,您还会得到最后的换行符。在比较之前,您需要将其从输入中删除。您会看到,如果您在输出周围加上引号:

print "Heads or tails? ";
my $guess = <>;
print "Your guess is '$guess'\n";

这将从输入中删除所有空格,这在您的情况下应该有意义:

$guess =~ s/\s*//mg;

不相关,但可能会让您感到困惑 --- 您

print "The right answer is ", $guess;

可能在它应该在的地方

print "The right answer is ", $answer;

最后,永远style,声明变量并始终将其放在脚本的顶部:

use strict;
use warnings;

When you read the answer, you also get the final newline. You need to remove it from the input before comparing. You'll see that if you put quotes around the output:

print "Heads or tails? ";
my $guess = <>;
print "Your guess is '$guess'\n";

This will remove all spaces from the input, which should make sense in your case:

$guess =~ s/\s*//mg;

Unrelated, but may confuse you --- you have

print "The right answer is ", $guess;

where it should probably be

print "The right answer is ", $answer;

Finally, for good style, declare your variables and always put this at the top of your script:

use strict;
use warnings;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文