Re: how?
Re: how?
- Subject: Re: how?
- From: Adam Morris <email@hidden>
- Date: Wed, 13 Jan 2010 21:37:44 -0500
I find that code misleading because it implies that that test will actually be used to exit the loop, but it isn’t.
A "lonely" repeat statement collides with Applescript's only way to ensure the loop executes once. I agree with you on the whitespace though.
I would write this code this way:
set gotValidInput to false
set possibles to {1, 5, 10}
set userInput to "default"
repeat until gotValidInput
set whatever to text returned of (display dialog "prompt" default answer userInput)
set whatever to whatever as number
set gotValidInput to (whatever is in possibles)
set userInput to whatever
end repeat
return userInput
On Wed, Jan 13, 2010 at 7:59 PM, Chris Page
<email@hidden> wrote:
On Jan 12, 2010, at 3:52 AM, Adam Morris wrote:
FYI this is a programmer's way of writing code that's partially a comment. Take a look at the repeat statement:
repeat until ValidInput = true
That means: "I'm going to keep asking the user for valid input." More experience in scripts will show you that you use this trick often: "Repeat forever until I get valid input."
Usually you'll just see "repeat until ValidInput"
It's the return statement that ends the otherwise endless loop.
And, as a comment, I find that code misleading because it implies that that test will actually be used to exit the loop, but it isn’t. I recommend changing it to just “repeat … end repeat”, which makes it clearer to the reader that the loop is exited due to some code inside the loop.
Also, I prefer to put whitespace around (above and below) return statements so that changes in flow control stand out:
if (NoRange > 2) and (NoRange < 60) then
set NoRange to NoRange as integer --Convert the player input
--Question: Why do I need this step???? A: see page 101 “Coercion”
return NoRange -- Return the player's input
else -- Display an error message if the input is not valid
--
Chris Page
The other, other AppleScript Chris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
- Follow-Ups:
- Re: how?
- From: Chris Page <email@hidden>
References: | |
| >Re: how? (From: Adam Morris <email@hidden>) |
| >Re: how? (From: Chris Page <email@hidden>) |