As a general point this game isn't going to be easy, even if you know the expected answers, the:
is requiring the guess to have the exact capitalisation of the answer and won't tolerate extra white-space or any other deviation from the expected string. Related to that is what FunkyMonk has already suggested, which is the possible presence of a newline on the end of $guess. Preprocessing $guess and/or using a form of regular expression to do the matching will definitely help.if ($guess eq $correct[$i])
In passing I notice:
which might be better written as:@incorrect = (@incorrect, $guess);
and also:push @incorrect, $guess ;
which I rather doubt is what you meant. It sets $grade to 66.7, because length doesn't give the length of an array, it gives the length of a string in characters. So why does it give an answer at all when asked for the length of an array ?... well, length(my @incorrect) gives 1 because the value of my @incorrect is an empty array, which when evaluated in scalar context gives the number 0, which when converted to a string is '0', which is 1 character long... This is Perl all over :-)my $grade = 100 - length(my @incorrect) * 33.3;
To get the number of entries in an array you just evaluate the array in scalar context... so: scalar(@incorrect) is probably what you wanted.
Finally, at the risk of being presumptious, if you are new to programming in general, I'd really advise you to study another language.
It's true that you can hack about in Perl and do some wonderful stuff quite economically. But, not only is it possible to pick up some truely disgusting habits, you need a grounding in something more formal if you are to really understand what Perl is doing.
Look at it this way, compared to most languages English has apparently little structure and few rules. This makes it relatively easy to learn and use, at least to get by. However, it is also hard to really master the subtleties of English, with all its irreglarity, conventions, idioms and other oddities; and to do that you do need some grammer and some formal teaching. Perl is like that.
In reply to Re: Control Structure problem, mistake can't be found
by gone2015
in thread Control Structure problem, mistake can't be found
by koolgirl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |