Это умная идея, чтобы посчитать слова в theClassString
(что было бы сделано с использованием number of words in theClassString
вместо простого words in theClassString
).Это будет работать большую часть времени, пока пользователь не включит "I&S"
в качестве одной из своих опций, что, к сожалению, считается двумя словами, "I"
и "S"
, поскольку амперсанд не является символом слова.
Вы также указали exit repeat
в неправильной половине блока if...then...else
, поскольку вы хотите разорвать цикл, когда пользователь выбирает 3 курса, а не когда он не выбирает 3 курса.
Скореечем пытаться привести результат выбора списка в строку, вам нужно просто посчитать количество элементов в результате, что можно сделать одним из трех способов:
count theClass
length of theClass
number in theClass
Вот переработанная версия вашего сценария:
property theClassList : {"Math ", "English ", "Science ", "I&S ", "Design "}
property text item delimiters : linefeed
set choices to missing value
repeat
if choices ≠ missing value then display dialog ¬
"You must select precisely three courses"
set choices to choose from list theClassList with prompt ¬
"Select three courses" with multiple selections allowed
if choices = false or the number of choices = 3 then exit repeat
end repeat
if choices = false then return
display dialog choices as text
... ИВот версия, которая использует рекурсивный обработчик вместо цикла повторения:
property theClassList : {"Math", "English", "Science", "I&S", "Design"}
property text item delimiters : linefeed
to choose()
tell (choose from list theClassList ¬
with prompt ("Select three courses") ¬
with multiple selections allowed) to ¬
if it = false or its length = 3 then ¬
return it
display dialog "You must select precisely three courses"
choose()
end choose
display alert (choose() as text)