Write code in PERL!!!! Create a variable and assign it the number between 1 and
ID: 3814537 • Letter: W
Question
Write code in PERL!!!!
Create a variable and assign it the number between 1 and 15 you want the user to guess Print a message on the screen "Guess the number game!" Prompt the user to their name ($name) Prompt the user to enter a number between 1 and 15 ($number) Code a loop so that the user can continue entering numbers if a wrong guess is entered Exit the loop only when the user either enters a 0 (zero) or the correct number Display these messages if the following occurs: a. If user enters 0, display "$name you entered $number. Exiting program" b. If user enters the correct number, display "$name you guessed the correct number! It is $number." c. If user enters a number higher than the correct number, display $number is TOO HIGH" (use capital letters) d. If user enters a number lower than the correct number, display "$number is too low"Explanation / Answer
code:
#
# Pseudo code:
# Generate a random number ($guess) for guessing.
# Print the title
# Prompt user for name ($name) and read the name without newline character
# Enter infinite loop to play the game
# Prompt user to enter a number ($number) between 1 to 15 and read the number
# If $number is zero
# print error message and leave the loop
# Else if $number is equal to $guess
# display success message and exit the loop
# Else if $number is less than $ guess
# print too low and repeat for loop
# Else
# print TOO HIGH and repeat for loop
#
$guess = 1 + int(rand(15));
print "Guess the number game! ";
print "Please enter your name: ";
$name = <STDIN>;
chomp($name);
for (;;)
{
print "Please enter a number between 1 to 15: ";
$number = int(<STDIN>);
if ($number == 0)
{
print "$name you entered $number. Exiting program ";
last;
}
elsif ($number == $guess)
{
print "$name you guessed the correct number! It is $number ";
last;
}
elsif ($number < $guess)
{
print "$number is too low ";
}
else
{
print "$number is TOO HIGH ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.