Find the answer to your Linux question:
Results 1 to 3 of 3
Create a script which ask the user to answer a mathematical equation. For example 5 + 5 = ?. You script should show the user the question and then evaluate ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Banned
    Join Date
    Jun 2007
    Posts
    29

    Perl Question 5 + 5 = answer


    Create a script which ask the user to answer a mathematical equation. For example 5 + 5 = ?. You script should show the user the question and then evaluate their response. If the answer is correct echo "Your are correct" if the answer is wrong echo "The answer was incorrect" and echo the answer .

    What I have so far.

    #!/usr/bin/perl

    if {($x + $y),
    print "content-type: text/html \n\n"; #HTTP HEADER

    #PICK TWO NUMBERS
    $x = 5;
    $y = 5;
    $z = 10;
    #ADDITION OPERATOR
    $area = ($x + $y);
    print $area;
    print "<br />";
    print "answer is correct";
    }
    {
    else {print "answer is incorrect";}

    exit 3
    }

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by renegadeviking View Post
    Create a script which ask the user to answer a mathematical equation. For example 5 + 5 = ?. You script should show the user the question and then evaluate their response. If the answer is correct echo "Your are correct" if the answer is wrong echo "The answer was incorrect" and echo the answer .
    Hi,

    Since you are sending an HTTP header in your code, i am guessing this needs to be a CGI/webpage interface?

    Assuming that is the case, I've attempted a stab at what you want, using a CGI form as both the input and calculation of your problem. If it is not, and just needs to be a shell script, then instead of the CGI portion, you'd read the user input from STDIN (but the rest of the code is reusable). If you don't know how to read STDIN in perl, just say the word.

    I've commented the code to attempt to explain what it is doing. Give it a whirl.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use CGI;
    
    # define your two constant numbers here
    my $x = 5;
    my $y = 5;
    
    # get CGI parameters passed in query string
    my $cgi = new CGI;
    
    # the CGI parameter containing the number passed to the script via a form
    my $num  = $cgi->param('num');
    
    # send the HTTP header no matter what
    print "Content-type: text/html\n\n"; #HTTP HEADER
    
    # see if something was submitted
    if(defined($num)){
    
      # make sure a number was provided
      if($num !~ /^[0-9]*$/){
        print "\`$num' is not a number!\n";
      }else{
    
        # see if provided number is the sum of x and y
        if($x + $y == $num){
          print "answer is correct\n";
        }else{
          print "answer is incorrent\n";
        }
      }
    
      # provide a link back to main form
      print "<p><a href='?'>Reset</a>\n";
    
    # generate an HTML form requesting the answer (a number) from user input
    }else{
      print <<"EOF";
    <form action='?' method='GET'>
    $x + $y = 
    <input type='text' name='num' value='' size='5' maxlength='5' /></br>
    <input type='submit' value='Submit' />
    </form>
    EOF
    }

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    This sounds a lot like a homework question, which are not allowed on this forum. If you have specific questions, feel free to ask them, but we won't do your homework for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •