I need to find a way to take a number, then ask the user to guess the number, then for the script to decide if it was within 100 of the original number.
Perhaps something like
set targetNumber to random number from 1 to 2000
set userGuess to text returned of (display dialog "guess a number" default answer "")
try
set errorSize to targetNumber - userGuess
if errorSize < 0 then set errorSize to -errorSize
on error
set errorSize to 999
end try
if errorSize < 100 then
display dialog "Hurrah! you are good guesser"
else
display dialog "too bad!"
end if
Ah,
I was having a quick play with this also
set theNumber to random number from 1 to 10023
set theNumberRange to 100
set def_Dialog to "Enter A Number"
set nope to "nope"
set ooYer to "oo-Yeah"
global def_Dialog, theNumberRange, theNumber, nope, ooYer
my doNum()
on doNum()
display dialog def_Dialog default answer "" buttons {"Cancel", "OK"} default button 2
copy the result as list to {text_returned, button_pressed}
try
set numb to text_returned as number
if numb ≤ (theNumber + theNumberRange) then
if numb > (theNumber - theNumberRange) then
say ooYer
else
display dialog nope
end if
else
display dialog nope
end if
on error
set def_Dialog to "I SAID Enter A Number,.. Doh"
beep
my doNum()
end try
end doNum
Did forget about it being a random number, and so was working with a fixed number, Thanks mikerickson, just spotted that before I posted.
You may note the quick and dirty way to ensure a number and not a none numeric entry.
Edit** forgot to add a line “set def_Dialog to “I SAID Enter A Number,… Doh””
Here’s a function I personally use all of the time:
on isNaN(ThingToCheck)
--Returns true if NOT a number
try
set ThingToCheck to (ThingToCheck as number)
return false
on error
return true
end try
end isNaN
It’s short, simple, and quick.
-SuperScripter
For anyone not sure what SuperScripter is giving an example to…
Its putting the type of check I have used in my script above for checking if the input is a number and not something else like a letter.
Putting things into a Handler/function like this is a better way to do somethings, as you have more scope and control over your main script…
I was just showing how I personally check if a result comes out as a number or not.
In my opinion, your version looked a bit cluttered and messy for regular use, so I put this one up as another method of doing it. I’m not saying yours didn’t work, I’m just saying “Hey, this is light and easy, and you can very quickly transfer it to other scripts for later use.”
That’s all. Sorry to confuse anyone.
-SuperScripter
I guess I should laugh…
I was not writing it for regular use
For one I never use User input of numbers regularly.
And more importantly I just quickly wrote it off the top of my head to help with that particular request while doing the many other things of the day, and I try not to only post when i have the script being all elegant and stuff.
Please post a tidy version of the whole script with the same functions.
You put up a small handler to part of the script with no explanation? Not a version of it.
Now Im not sure how you read that, but Its saying “Putting things into a Handler/function like this is a better way to do somethings…”
I.e the way you did it.
But that does not mean it is better in this case.
Hi,
mikerickson,
I noticed that your script was rounding to 99 rather than 100
I think all that is needed ti to change
if errorSize < 100 then
to
if errorSize ≤ 100 then
I changed it up a little. Nothing different.
set theNumber to random number from 1 to 10023
set theNumberRange to 100
set def_Dialog to "Enter A Number"
set no_ to "No. You'll never guess. It was " & theNumber
set yes_ to "OO-Yeah"
global def_Dialog, theNumberRange, theNumber, no_, yes_
my doNum()
on doNum()
display dialog def_Dialog default answer (theNumber + (random number from 99 to 293)) buttons {"Cancel", "OK"} default button 2
copy the result as list to {text_returned, button_pressed}
try
set numb to text_returned as number
if numb ≤ (theNumber + theNumberRange) then
if numb ≥ (theNumber - theNumberRange) then
say yes_
else
display dialog no_
end if
else
display dialog no_
end if
on error
set def_Dialog to "I said A NUMBER! Duh..." & return & "Think... A number is something like " & theNumber
beep
my doNum()
end try
end doNum
NOTE: Yeah, you can find the answer by making a mistake.
Hi.
In the second line, the record returned by ‘display dialog’ is coerced to list and the two items of the result are copied to variables.
- Records are labelled, not ordered. If you coerce a record to list, you should never assume that the items in the list will be in any particular order, even if, as in this case, “they always are”. You should use the record’s labels to ensure that the values go to the right variables.
display dialog def_Dialog default answer "" buttons {"Cancel", "OK"} default button 2
set {text_returned, button_pressed} to {text returned, button returned} of result
Or:
display dialog def_Dialog default answer "" buttons {"Cancel", "OK"} default button 2
set {text returned:text_returned, button returned:button_pressed} to result
- In the above code, of course, if the line after ‘display dialog’ is reached, there’s only one possible value for button_pressed ” and it’s not used in the script anyway.
This can be simplfied to:
if (numb ≤ (theNumber + theNumberRange)) and (numb ≥ (theNumber - theNumberRange)) then
say yes_
else
display dialog no_
end if
Hi Nigel,
I did actually get that issue at one point, which I had not seen before. So thanks for clearing that up for me.
I will try and use the method you pointed out.
Although to be honest, I never usually type them out myself, but use the apple stock code included in the Scripts folder.??, so two lessons learnt there.:rolleyes:
Cheers.