# 使用Perl的数字猜谜游戏

1. 兰德n： 此函数用于生成0到n之间的随机数。此函数始终返回浮点数。因此，它的结果被显式转换为整数值。
2. Chomp（）： 此函数用于从用户输入中删除换行符。

 ` # Number Guessing Game implementation ` ` # using Perl Programming ` ` ` ` print ` ` "Number guessing game" ` ` ; ` ` ` ` # 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; ` ` \$n ` ` = 0; ` ` ` ` print ` ` "Guess a number (between 0 and 10): " ` ` ; ` ` ` ` # 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 ` ` = ); ` ` ` ` ` ` ` ` # 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}" ` ` ; ` ` ` ` } ` ` ` ` ` ` # The user entered number is ` ` ` ` # greater than the generated ` ` ` ` # number ` ` ` ` else ` ` ` ` { ` ` ` ` print ` ` "Your guess was too high," ` ` ; ` ` ` ` print ` ` " guess a lower number than \${userinput}" ` ` ; ` ` ` ` } ` ` ` ` ` ` # 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}." ` ` ; ` ` ` ` } ` ` ` ` `

```5
6
8
9
```

```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
```