RosettaCodeData/Task/Guess-the-number-With-feedback/Cache-ObjectScript/guess-the-number-with-feedback.cos
2020-02-17 23:21:07 -08:00

29 lines
882 B
Text

GUESSNUM
; get a random number between 1 and 100
set target = ($random(100) + 1) ; $r(100) gives 0-99
; loop until correct
set tries = 0
for {
write !,"Guess the integer between 1 and 100: "
read "",guess ; gets input following write
; validate input
if (guess?.N) && (guess > 0) && (guess < 101) {
; increment attempts
set tries = tries + 1
; evaluate the guess
set resp = $select(guess < target:"too low.",guess > target:"too high.",1:"correct!")
; display result, conditionally exit loop
write !,"Your guess was "_resp
quit:resp="correct!"
}
else {
write !,"Please enter an integer between 1 and 100."
}
} ; guess loop
write !!,"You guessed the number in "_tries_" attempts."
quit