Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.09 KB | None | 0 0
  1. start:
  2. $mynum = int(rand(100));
  3. $mynum = $mynum +1;
  4. print "Game started! \nThe objective of the game is to work out which number I'm thinking of. \nThe number is between 1 and 100. \nStart guessing!\n";
  5. print "For development purposes, the number is $mynum\n";
  6. userinput:
  7. $myguess = <>;
  8. if ($myguess =~ m/^\s*\d+\s*$/) {
  9.   if ($myguess > $mynum) {
  10.     print "Your guess was too high! \nTry a lower number.\n";
  11.     goto userinput;
  12.   }
  13.   elsif ($myguess < $mynum) {
  14.     print "Your guess was too low! \nTry a higher number.\n";
  15.     goto userinput;
  16.   }
  17.   elsif ($myguess == $mynum) {
  18.     print "That was the number I was thinking of!\n";
  19.     playagain:
  20.     print "Play again? (y/n)\n";
  21.     yesno:
  22.     $yesno = <>;
  23.     if ($yesno != m/^\s*(y|n|yes|no)\s*$/i) {
  24.       print "That's not a valid response\n";
  25.       goto playagain;
  26.     }
  27.     elsif ($yesno =~ m/^\s*(y|yes)\s*$/i) {
  28.       goto start;
  29.     }
  30.     elsif ($yesno =~ m/^\s*(n|no)\s*$/i) {
  31.       exit 0;
  32.     }
  33.   }
  34. }
  35. else {
  36.   print "That's not a valid input. \nPlease input a number between 1 and 100.\n";
  37.   goto userinput;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement