I am trying to write a very simple program that asks the user a question and displays a response depending if they get the answer right or not. The code below does this fine but I want it to loop back to the question again if they get the answer wrong. Any help much appreciated as this is my first attempt at any Perl (as you can probably tell from the basic coding!)

#!/usr/bin/perl #define a variable to store the number of planets $noplanets = 9; #displays the question to the user print "How many planets are there?"; #sets the variable $planets to whatever is input by the user and rem +oves the line break chomp ($planets=<>); #checks to see if the user has input any incorrect answer if ($planets ne $noplanets){ #tells the program to display the text in quotation marks print "Sorry thats not right, try again\n"; #tells the program where the end of the if statement is } #tells the program what to display if the answer is not incorrect else { print "Well done there are indeed $noplanets planets in the + solar system"; #ends the else statement }

In reply to Help using a while loop with STDIN by amonline

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.