# Number Guessing Game using Perl

The Number guessing game is all about guessing the number randomly chosen by the computer in given number of chances.

**Functions to be used :**

**rand n :**This function used to generate a random number between 0 to n. And this function always returns the floating point number. So its result is converted to integer value explicitly.**Chomp() :**This function is used to remove the newline character from the user input.

**Explanation:**

In the program, while loop runs until the number guessed by the user is equal to the generated number or the number of attempts are less than the maximum number of chances. If the number of attempts becomes greater than the number of chances the game stops and the user loses the game. If the user guesses the correct number in given number of chances then he or she will win. After every guess made by the user, the program informs the user whether the guessed number was smaller, greater than the actual generated number.

In this code, initially, **rand function** chooses a random number as x. The function (rand k) finds a random number between 0 and k. As this random number is a floating point number, so **“int”** is used to convert it explicitly to the whole number. x stores the whole number. The user is given a specific number of chances to guess the number if the chances exceed the user guesses the user would lose.

Below is the implementation :

`# Number Guessing Game implementation ` `# using Perl Programming ` ` ` `print` `"Number guessing game\n"` `; ` ` ` `# rand function to generate the ` `# random number b/w 0 to 10 ` `# which is converted to integer ` `# and store to the variable "x" ` `$x` `= ` `int` `rand` `10; ` ` ` `# variable to count the correct ` `# number of chances ` `$correct` `= 0; ` ` ` `# number of chances to be given ` `# to the user to guess number ` `# the number or it is the of ` `# inputs given by user into ` `# input box here number of ` `# chances are 4 ` `$chances` `= 4; ` ` ` `print` `"Guess a number (between 0 and 10): \n"` `; ` ` ` `# while loop containing variable n ` `# which is used as counter value ` `# variable chance ` `while` `(` `$n` `< ` `$chances` `) ` `{ ` ` ` ` ` `# Enter a number between 0 to 10 ` ` ` `# Extract the number from input ` ` ` `# and remove newline character ` ` ` `chomp` `(` `$userinput` `= <STDIN>); ` ` ` ` ` ` ` `# To check whether user provide ` ` ` `# any input or not ` ` ` `if` `(` `$userinput` `!= ` `"blank"` `) ` ` ` `{ ` ` ` ` ` `# Compare the user entered number ` ` ` `# with the number to be guessed ` ` ` `if` `(` `$x` `== ` `$userinput` `) ` ` ` `{ ` ` ` ` ` `# if number entered by user ` ` ` `# is same as the generated ` ` ` `# number by rand function then ` ` ` `# break from loop using loop ` ` ` `# control statement "last" ` ` ` `$correct` `= 1; ` ` ` `last` `; ` ` ` `} ` ` ` ` ` `# Check if the user entered ` ` ` `# number is smaller than ` ` ` `# the generated number ` ` ` `elsif` `(` `$x` `> ` `$userinput` `) ` ` ` `{ ` ` ` `print` `"Your guess was too low,"` `; ` ` ` `print` `" guess a higher number than ${userinput}\n"` `; ` ` ` `} ` ` ` ` ` `# The user entered number is ` ` ` `# greater than the generated ` ` ` `# number ` ` ` `else` ` ` `{ ` ` ` `print` `"Your guess was too high,"` `; ` ` ` `print` `" guess a lower number than ${userinput}\n"` `; ` ` ` `} ` ` ` ` ` `# Number of chances given ` ` ` `# to user increases by one ` ` ` `$n` `++; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `$chances` `--; ` ` ` `} ` `} ` ` ` ` ` `# Check whether the user ` ` ` `# guessed the correct number ` ` ` `if` `(` `$correct` `== 1) ` ` ` `{ ` ` ` `print` `"You Guessed Correct!"` `; ` ` ` `print` `" The number was $x"` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `print` `"It was actually ${x}."` `; ` ` ` `} ` ` ` ` ` |

*chevron_right*

*filter_none*

**Inputs:**

5 6 8 9

**Output:**

Number guessing game Guess a number (between 0 and 10): Your guess was too low, guess a higher number than 5 Your guess was too low, guess a higher number than 6 Your guess was too low, guess a higher number than 8 You Guessed Correct! The number was 9

**Note:** In above program user can modify the value of **rand** function to increase the range of numbers in this game and also user can increase the number of chances by increasing the value of the chance variable.

## Recommended Posts:

- Perl | Number and its Types
- Perl | Basic Syntax of a Perl Program
- Perl | Getting the Number of Elements of an Array
- Perl | Automatic String to Number Conversion or Casting
- Perl vs C/C++
- Perl | Operators | Set - 1
- Perl vs Java
- Perl | exp Function
- Perl | Hashes
- Perl | y Operator
- Perl | tr Operator
- Perl vs Python
- Perl | given-when Statement
- Perl | Modules
- Perl | lt operator

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.