#Hailey Hanson
#October 30th, 2013
#Assignment 5:A high-low script
#this script should prompt the user to think of a number between 1 and 100
#the script will then make guesses about the number and the user will tell the program if the guess is too high or too low
#initialize high, low and middle values
let h=100
let l=1
let g=$((( $h + $l ) / 2))
#ask questions. keep name
echo Please type your name
read name
echo Think of a number between 1 and 100
echo Is your number $g? If too high, enter h. If too low, enter l. If correct, enter c.
read text #text will hold h, l or c
let sum=1 #initialization of total number of tries
for i in `seq 0 $sum`; #to keep counter on sum
do
while [ $text != c ]; do #so technically, if the user were to enter anything other than l or h, my code would treat it as if the user entered c.
#{
if [ $text == h ] ; then #if guess is too high, make high value half of its value-1, count sum.
let h=$g-1
let g=$((( $h + $l )/2))
echo Is your number $g? If too high, enter h. If too low, enter l. If correct, enter c.
read text
let sum=$sum+1
fi
if [ $text == l ] ; then #if guess is too low, make low value half of high value+1, count sum.
let l=$g+1
let g=$((( $h + $l )/2))
echo Is your number $g? If too high, enter h. If too low, enter l. If correct, enter c.
read text
let sum=$sum+1
fi
#}
done #end of while loop
done #end of for loop/sum counter
echo You number is $g! Thanks for playing, $name
echo It took me $sum tries to guess your number.
echo I\'m feeling extra savvy on this day, `date`
echo "On `date` It took $sum tries to guess $name's number.
" >> highlow.log
No comments:
Post a Comment