Find the answer to your Linux question:
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 ...
  1. #1
    Just 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 

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  3. #3
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    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

  4. #4
    Just Joined!
    Join Date
    May 2008
    Posts
    4
    Quote Originally Posted by smolloy View Post
    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?
    yes I believe I understand you, the idea is to have the user keep guessing until they get it right...and to help them by saying if their guess is greater or less thant the number...
    and this is due tonite/tomorrow....

  5. #5
    Just 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 [ ]
    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


  6. #6
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...