In: Computer Science
Linux: I have a working script but need a high/low comparison added to each guess by the user.
choice=$(( $RANDOM % 100 + 1 ))
read -p 'Please choose a number from 1-100:' variable
while [ $variable -ne $choice ];
do
echo "Your choice was wrong. Please try again."
read -p 'Please choose a number from 1-100:' variable
done
echo "You chose correctly!!!"
The comparison is the variable chosen by the game is supposed to be guessed by the user. Because the choices range from 1-100, I need the script to tell the user if they chose too high or too low until the user makes the correct choice.
Code:
choice=$(( $RANDOM % 100 + 1 ))
variable=0
while [ $variable -ne $choice ];
do
read -p 'Please choose a number from 1-100:' variable
# if variable is greater than choice
if [ $variable -gt $choice ]
then
echo "Your choice is too high. Please try again"
# if variable is less than choice
elif [ $variable -lt $choice ]
then
echo "Your choice is too low. Please try again"
# if variable equals choice
else
echo "You chose correctly!!!"
fi
done
Code Screenshot:
Sample Output: