Results 1 to 6 of 6
Hello,
I am trying to write a script where the user has to guess the random number... the counter is suppose to count how many guesses the user takes...but i ...
- 05-13-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 4
Script that allows user to guess random number
Hello,
I am trying to write a script where the user has to guess the random number... the counter is suppose to count how many guesses the user takes...but i just want it to work before i worry about the counter...can anyone help?? thank you
PHP Code:#!/bin/sh
clear
RANGE=60
num=$RANDOM
let "num %= $RANGE"
echo "Guess the number"
counter=0
read guess
while [ $guess != $num ]
do
if
[ $guess > $num ]
then
echo "$guess is greater than the number"
elif
[ $guess < $num ]
then
echo "$guess is less than the number"
else
[ $guess = $num ]
echo "$guess is Correct!!!"
fi
done
- 05-13-2008 #2
When does your instructor say this assignment is due?
And specifically what is your question about this script? Can you narrow it down more than "I just want it to work"?--
Bill
Old age and treachery will overcome youth and skill.
- 05-13-2008 #3
One problem I see is that, if you guess the number wrongly first time, then $guess will not equal $num (obviously). This is a problem since you have that inequality as the test for the while statement, meaning that it will sit in that loop forever.
You need to have the user guess the number again if she got it wrong, and that guess has to happen inside the while loop, so that you are testing against something new each time. Understand?Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
Need instant help? Try us on IRC -- #linuxforums on freenode
- 05-13-2008 #4Just Joined!
- Join Date
- May 2008
- Posts
- 4
- 05-13-2008 #5Just Joined!
- Join Date
- May 2008
- Posts
- 4
here is an update of where the script is... its a bash script..
PHP Code:#!/bin/bash
clear
RANGE=60
num=$RANDOM
let "num %= $RANGE"
counter=0
echo $num
while [ 1 ]
do
echo "enter a Guess"
read guess
if
[ $guess -eq num ]
then
echo "$guess is Correct Guess"
elif
[ $guess -lt num ]
then
echo "$guess is less than the number"
elif
[ $guess -gt num ]
then
echo "$guess is Greater than the number"
else
echo "Please enter a number"
fi
done
~
- 05-13-2008 #6
Pretty good. This will work -- except that you need to edit the tests to test against $num, not num.
Another problem is that, even when you get the number right, it still asks you to guess again. You need to break out of the while loop when the user guesses correctly.
Nice of you to "echo $num" at the top by the way. Makes the game a lot easier
Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
Need instant help? Try us on IRC -- #linuxforums on freenode


Reply With Quote
