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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |